我可以通过curl执行以下命令成功创建一个地方:
$ curl -vX POST https://server/api/v1/places.json -d "
auth_token=B8dsbz4HExMskqUa6Qhn& \
place[name]=Fuelstation Central& \
place[city]=Grossbeeren& \
place[address]=Buschweg 1& \
place[latitude]=52.3601& \
place[longitude]=13.3332& \
place[washing]=true& \
place[founded_at_year]=2000& \
place[products][]=diesel& \
place[products][]=benzin \
"
服务器返回HTTP/1.1 201 Created。
现在我想将有效负载存储在一个JSON文件中,如下所示:
// testplace.json
{
"auth_token" : "B8dsbz4HExMskqUa6Qhn",
"name" : "Fuelstation Central",
"city" : "Grossbeeren",
"address" : "Buschweg 1",
"latitude" : 52.3601,
"longitude" : 13.3332,
"washing" : true,
"founded_at_year" : 2000,
"products" : ["diesel","benzin"]
}
所以我修改命令执行如下:
$ curl -vX POST http://server/api/v1/places.json -d @testplace.json
返回HTTP/1.1 401 Unauthorized失败。为什么?
你可以通过——data-raw参数来压缩json文件的内容
curl https://api.com/route -H 'Content-Type: application/json'——data-raw "$(cat ~/.json/payload-2022-03-03。Json | grep -v '^\s*//')"
curl https://api.com/route -H 'Content-Type: application/json' -d @<(jq . json)~ / . json /载荷- 2022 - 03 - 03. - json)
curl https://api.com/route -H 'Content-Type: application/json' -d @<(jq '{"payload": .}' < ~/.json/payload-2022-03-03.json)
注意:json文件中的注释会被grep -v '^\s*//'过滤掉
您也可以使用grep或cat或jq通过stdin将数据传递给curl
Grep -v '^\s*//' ~/.json/payload-2022-03-03. Grep -v '^\s*//'json | curl https://api.com/route - h 'Content-Type: application/json' -d @-
猫~ / . json /载荷- 2022 - 03 - 03。json | grep -v '^\s*//' | curl https://api.com/route - h 'Content-Type: application/json' -d @-
金桥。~ / . json /载荷- 2022 - 03 - 03。json | curl https://api.com/route - h 'Content-Type: application/json' -d @-
Jq ` {"payload": .} ` < ~/.json/payload-2022-03-03。json | curl https://api.com/route - h 'Content-Type: application/json' -d @-