3

The first command works and the second doesn't. What do I need to change to allow the json array to work? Thank you in advance.

command #1:

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

command #2

curl -d '{"uid":"TEST","object":["server1", "server2", "server3"]}' \
        -H "Content-Type: application/json" \
        -X POST http://WEBSERVER/api/v1/inventory
3
  • 1
    For future note -- backticks are for code formatting strings which are less than a single line in length. Use the {} button in the editor with a multi-line block selected, or add four spaces before each line, for longer segments. This gets you colorized syntax highlighting and generally better-looking formatting. Commented Oct 12, 2017 at 17:26
  • 1
    That said, in terms of what content a server allows -- that's the server's decision; it's not something bash or curl has any say in. If you can reproduce a problem with a publicly-available service that is explicitly documented to allow arrays, that would put us in a better place (in terms of being able to reproduce a minimal reproducible example -- roughly, the shortest possible code that someone else can run to see a problem themselves, or use to test whether their proposed answer actually fixes a problem). Commented Oct 12, 2017 at 17:34
  • 1
    (Also, "doesn't work" is not much of a description to go off of -- it doesn't tell us if you got an error from curl itself or from the remote server, f/e, and that distinction is critical. Please be sure that you provide the exact error you're asking about, going forward). Commented Oct 12, 2017 at 17:35

1 Answer 1

2

Your curl syntax is fine, you could test by using httpbin.org, for example:

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

Returns:

{
  "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"
}

You indeed could pipe the result and verify the output with your input:

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

It will print:

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

Probably the server you are using to post data don't accept your request, check the returned status code, could give a clue, maybe is a 400 (bad request) or a 406 (not acceptable) etc, just in case here is a list of possible status codes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.