如何通过 JSON 文件传递 curl 的有效负载?

How to pass payload via JSON file for curl?

提问人:JJD 提问时间:9/4/2013 最后编辑:whirlwinJJD 更新时间:7/17/2022 访问量:372895

问:

我可以通过执行以下命令成功创建一个位置: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 \
"

服务器返回 .
现在我想将有效负载存储在 JSON 文件中,如下所示:
HTTP/1.1 201 Created

// 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

json 卷曲

评论

1赞 ahmet alp balkan 8/7/2019
另请记住,如果您要上传二进制文件,则应使用 .--data-binary
2赞 Chris Halcrow 6/1/2021
对于任何引用此问题来回答“如何指定包含 JSON 的文件”的人,请注意它带有问题中给出的符号,例如 .这假设您正在从包含testplace.json的目录运行 curl - 否则使用 例如@$ curl -vX POST http://server/api/v1/places.json -d @testplace.json@/some/directory/some.json

答:

446赞 mata 9/4/2013 #1

curl发送默认内容类型为 的 POST 请求。如果要发送 JSON 请求,则必须指定正确的内容类型标头:application/x-www-form-urlencoded

$ curl -vX POST http://server/api/v1/places.json -d @testplace.json \
--header "Content-Type: application/json"

但这只有在服务器接受 json 输入时才有效。url 末尾可能只表示输出是 json,并不一定意味着它也会处理 json 输入。API 文档应该会提示您是否这样做。.json

您收到错误而不是其他错误的原因可能是因为服务器无法从您的请求中提取。401auth_token

评论

11赞 Shadi 1/26/2017
我试图在之后使用并且遇到了麻烦,直到我从这个答案中了解到我可以使用.谢谢:)cat file.json-d@file.json
1赞 Chaim Eliyah 9/14/2017
请注意,如果您需要多个标头,则需要多次指定 /,至少在 Ubuntu 上的 bash 中进行测试时是这样。-H--header
9赞 Chris Halcrow 6/1/2021 #2

为了阐明如何实际指定包含要发布的 JSON 的文件,请注意它带有 OP 中所示的符号@

例如,本地 .NET Core API 的典型帖子:e.g. a typical post to a local .NET Core API:

curl -X POST https://localhost:5001/api -H "Content-Type: application/json" -d @/some/directory/some.json

2赞 Andrew 3/4/2022 #3

您可以通过参数将文件的内容catjsoncurl--data-raw

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/payload-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*//'

您还可以使用 or 或 将数据传递给curlstdingrepcatjq

grep -v '^\s*//' ~/.json/payload-2022-03-03.json | curl https://api.com/route -H 'Content-Type: application/json' -d @-

cat ~/.json/payload-2022-03-03.json | grep -v '^\s*//' | curl https://api.com/route -H 'Content-Type: application/json' -d @-

jq . ~/.json/payload-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 @-