用json数组卷曲

时间:2017-10-12 17:24:29

标签: json bash curl

第一个命令有效,第二个命令没有。我需要更改以允许json数组工作?提前谢谢。

命令#1:

curl -d '{"uid":"TEST", "object":"TEST"}' \
        -H "Content-Type: application/json" \
        -X POST http://WEBSERVER/api/v1/inventory

命令#2

curl -d '{"uid":"TEST","object":["server1", "server2", "server3"]}' \
        -H "Content-Type: application/json" \
        -X POST http://WEBSERVER/api/v1/inventory

1 个答案:

答案 0 :(得分:1)

您的curl语法很好,您可以使用httpbin.org进行测试,例如:

$ curl -d '{"uid":"TEST","object":["server1", "server2", "server3"]}' \
    -H "Content-Type: application/json" \
    -X POST http://httpbin.org/post

返回:

{
  "args": {},
  "data": "{\"uid\":\"TEST\",\"object\":[\"server1\", \"server2\", \"server3\"]}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Connection": "close",
    "Content-Length": "57",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": {
    "object": [
      "server1",
      "server2",
      "server3"
    ],
    "uid": "TEST"
  },
  "origin": "x.x.x.x",
  "url": "http://httpbin.org/post"
}

您确实可以输出结果并使用您的输入验证输出:

$ curl -d '{"uid":"TEST","object":["server1", "server2", "server3"]}' \
    -H "Content-Type: application/json" \
    -X POST http://httpbin.org/post -s | jq -r '.data'

它将打印:

{"uid":"TEST","object":["server1", "server2", "server3"]}

您用来发布数据的服务器可能不接受您的请求,检查返回的状态代码,可能提供线索,可能是400(不良请求)或406(不可接受)等,以防万一这是可能的status codes.

列表