使用curl使用bashscript发布json

时间:2019-04-10 12:13:56

标签: 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",/
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

1 个答案:

答案 0 :(得分:2)

就像@Aaron所说,shell正在拆分您的值,因为其中有空格。默认情况下,Bash按空格,制表符和换行符分隔“单词”。 "weak quotes"(双引号)将跳过此分词过程,同时仍允许您扩展变量。

curl -d "$param" ...