使用curl使用bashscript发布json
我想使用curl在bash脚本中发布一个json,但我得到的错误取决于内容 我犯了一个错误使用curl使用bashscript发布json,json,bash,curl,Json,Bash,Curl,我想使用curl在bash脚本中发布一个json,但我得到的错误取决于内容 我犯了一个错误 Rebuilt URL to: "major":"1221",/ Illegal port number Closing connection -1 curl: (3) Illegal port number Note: Unnecessary use of -X or --request, POST is already inferred. Rebuilt URL to: "minor":"32112"
Rebuilt URL to: "major":"1221",/
Illegal port number
Closing connection -1
curl: (3) Illegal port number
Note: Unnecessary use of -X or --request, POST is already inferred.
Rebuilt URL to: "minor":"32112",/
Illegal port number
Closing connection -1
curl: (3) Illegal port number
curl: (3) [globbing] unmatched close brace/bracket in column 48
这很有效
#!/bin/bash
param='[{"timestamp":"value","sourceId":"fe28edab963de6788"}]'
echo $param
curl -d $param -H "Content-Type: application/json" -H "X-Security-AuthKey: 84C0712F-856D-4EC7-B136-FA39B5FFE995" -H "Type: DATA" -H "Device: RaspberryPI" -X POST "https://test.api.com/webhook" -v
但这不起作用
#!/bin/bash
param='[{"timestamp":"1554895106","sourceId":"fe28edab963de6788","uuid":"F7826DA6-4FA2-4E98-8024-BC5B71E0893E", "major":"1221", "minor":"32112", "rssi":"-63","distance":".26307557616382837295"}]'
echo $param
curl -d $param -H "Content-Type: application/json" -H "X-Security-AuthKey: 84C0712F-856D-4EC7-B136-FA39B5FFE995" -H "Type: DATA" -H "Device: RaspberryPI" -X POST "https://test.api.com/webhook" -v
就像@Aaron所说的,外壳正在分割你的价值,因为它有空间。默认情况下,Bash按空格、制表符和换行符拆分“单词”<代码>“弱引号”(双引号)将跳过此分词过程,同时仍允许您展开变量
curl -d "$param" ...
就像@Aaron所说的,外壳正在分割你的价值,因为它有空间。默认情况下,Bash按空格、制表符和换行符拆分“单词”<代码>“弱引号”(双引号)将跳过此分词过程,同时仍允许您展开变量
curl -d "$param" ...
在curl命令中的$param
周围添加“双引号”。您的json肯定被视为多个单词,第一个单词与-d
关联,但下面的单词在curl命令中被解析为$param
周围的urlsad“双引号”。您的json肯定被视为多个单词,第一个单词与-d
关联,但后面的单词被解析为URL