8

I have read several posts about this topic but i couldn't figure out how to send json data with curl POST on windows 10 (powershell).

I tried with \" or with """ nothing. json data:

{
    "frames": [
        {
            "text": "HOME2",
            "icon": "i294",
            "index": 0
        },
        {
            "text": "? 65",
            "icon": null,
            "index": 1
        }
    ]
}

Example of curl tries:

> curl -H "Accept: application/json" -H "X-Access-Token: xyz" -X POST "https://xyz" -d "{ """frames""": [{ """text""": """HOME2""", """icon""": """i294""", """index""": 0 }, { """text""": """? 65""", """icon""": null, """index""": 1 }]}"
{"error":{"code":null,"message":"Bad Request","trace":["request body must not be empty"]}}curl: (3) [globbing] bad range specification in column 2
curl: (6) Could not resolve host: text
curl: (6) Could not resolve host: HOME2,
curl: (6) Could not resolve host: icon
curl: (6) Could not resolve host: i294,
curl: (6) Could not resolve host: index
curl: (6) Could not resolve host: 0
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: text
curl: (6) Could not resolve host: ? 65,
curl: (6) Could not resolve host: icon
curl: (6) Could not resolve host: null,
curl: (6) Could not resolve host: index
curl: (6) Could not resolve host: 1
curl: (3) [globbing] unmatched close brace/bracket in column 1

with \"

> curl -H "Accept: application/json" -H "X-Access-Token: xyz" -X POST "https://xyz" -d "{ \"frames\": [ { \"text\": \"HOME2\", \"icon\": \"i294\", \"index\": 0 }, { \"text\": \"? 65\", \"icon\": null, \"index\": 1 } ] }"
{"error":{"code":null,"message":"Bad Request","trace":["request body must not be empty"]}}curl: (3) [globbing] bad range specification in column 2
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: \text\
curl: (6) Could not resolve host: \HOME2\,
curl: (6) Could not resolve host: \icon\
curl: (6) Could not resolve host: \i294\,
curl: (6) Could not resolve host: \index\
curl: (6) Could not resolve host: 0
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: \text\
curl: (6) Could not resolve host: \? 65\,
curl: (6) Could not resolve host: \icon\
curl: (6) Could not resolve host: null,
curl: (6) Could not resolve host: \index\
curl: (6) Could not resolve host: 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (3) [globbing] unmatched close brace/bracket in column 1

What am i doing wrong?

5 Answers 5

3

So, after further reading and from the answer from mohsen, I finalized my command line to:

curl -H "Accept: application/json" -H "X-Access-Token: xyz" -X POST "https://xyz" -d @exported.json

I added the json into a file called exported.json and now it is more compact and it still works.

The json data it self didn't need much more tweaking, see exported.json file:

{"frames":[{"text":"HOME2","icon":"i294","index":"0"},{"text":"? 65","icon":"null","index":"1"}]}
1
  • 1
    On windows cmd prompt, curl really wants the @ before the path to the file. Commented Aug 14, 2022 at 14:28
3

Looks like Windows command line and PowerShell messes with curl and JSON post data. Tried the following in CMD, PowerShell and WSL. WSL gives the result I expected, the other two fails. (originally tried to post data to Node-Red, and their official example failed, that was my WTF moment)

Try this (httpbin.org just echoes your data back this case):

curl -X POST -d '{"name":"Nick","a":32}' -H "Content-type:application/json" https://httpbin.org/anything

Response when called from WSL:

{
  "args": {},
  "data": "{\"name\":\"Nick\",\"a\":32}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "22",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.71.1"
  },
  "json": {
    "a": 32,
    "name": "Nick"
  },
  "method": "POST",
  "origin": "*********",
  "url": "https://httpbin.org/anything"
}

Response when called from CMD / PS:

{
  "args": {},
  "data": "'{name:Nick,a:32}'",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "18",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.55.1"
  },
  "json": null,
  "method": "POST",
  "origin": "****",
  "url": "https://httpbin.org/anything"
}

Notice Content-Length is different and the json property in the response is null when called from cmd or ps.

But doing this does work from both WSL and cmd, but not from PowerShell: curl -d "{\"name\":\"Nick\",\"a\":32}" -H "Content-type:application/json" https://httpbin.org/anything

And this works in PowerShell but fails in the other two curl -d '{\"name\":\"Nick\",\"a\":32}' -H "Content-type:application/json" https://httpbin.org/anything

So yes, put your post data in a file. But of course, powershell has it's own rules, so in the others you can do -d @data.json, but in powershell you must fo -d `@data.json (backtick before the @ symbol)

1

first minify your json here, don't use pretty json.

try this:

curl -d 'curl -d '{"frames":[{"text":"HOME2","icon":"i294","index":0}, 
 {"text":"? 65","icon":null,"index":1}]}' -H 'Content-Type: application/json' 
 https://example.com/login

for more info check this site

4
  • This helped but only cygwin windows. On windows powershell it still raises the error.
    – FotisK
    Commented May 6, 2020 at 7:02
  • Your example yes, but if i use my json data set it still complains in windows powershell.
    – FotisK
    Commented May 6, 2020 at 7:07
  • I updated my question with my latest findings and what works now.
    – FotisK
    Commented May 7, 2020 at 11:19
  • don't update question just answer your question to make community better
    – Mohsen
    Commented May 7, 2020 at 11:31
0

I thought I would share my findings on this in case it helps others.

For me, to get this to work, I simply had to toggle the single and double quotes, ironically, making the JSON not correctly formatted:

DOES NOT WORK:

curl -X POST -H 'Content-type: application/json' -d '{"text":"Hello World"}' https://my-unique-slack-hooks-url-here.com

DOES WORK:

curl -X POST -H 'Content-type: application/json' -d "{'text':'Hello, World'}" https://my-unique-slack-hooks-url-here.com
0

For those who still encounter this kind of problem in Windows, use instead :

Invoke-RestMethod -Uri http://your-url:port/your-end-point -Method POST(or other) -ContentType "application/json" -InFile .\your-json-file

It worked for me

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.