第一个命令有效,第二个命令无效。我需要更改什么才能允许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

最佳答案

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.列表

10-08 11:22