How to send POST request with json body via curl from windows batch script

dKab picture dKab · Mar 14, 2017 · Viewed 16.4k times · Source

I'm writing batch script which should send request with json.

call curl -X POST -H 'Content-type: application/json' --data '{"text": "Pull requests:\n%linksText% has been deployed to %stagingServerUrl%", "username": "Staging Server"}' http://requestb.in/ovehwtov

I run my script from git bash and alhought it sends the request, the body is malformed and just before it sends the request I see errors in console:

curl: (6) Couldn't resolve host 'application'
curl: (6) Couldn't resolve host '"Pull'
curl: (6) Couldn't resolve host 'requests:\nhttp'
curl: (6) Couldn't resolve host 'has'
curl: (6) Couldn't resolve host 'been'
curl: (6) Couldn't resolve host 'deployed'
curl: (6) Couldn't resolve host 'to'
curl: (6) Couldn't resolve host 'unicorns2url",'
curl: (6) Couldn't resolve host '"username"'
curl: (6) Couldn't resolve host 'Staging'
curl: (3) [globbing] unmatched close brace/bracket in column 8
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    11    0     2  100     9      6     30 --:--:-- --:--:-- --:--:--    30ok

Here's how body looks:

'{"text":

which isn't right.

As you can see I'm composing the json from variables, but it's not the cause of failure: when I remove them and call

call curl -X POST -H 'Content-type: application/json' --data '{"text": "Pull requests has been deployed to", "username": "Staging Server"}' http://requestb.in/ovehwtov

The same error happens.

However when I copy this command from my batch script and paste it directly into git bash console It works seamlessly. Don't ask me why I'm running windows batch script from git bash, when I could write bash script using bash language instead of awkward and confusing DOS syntax. I didn't realize that when I started writting the script and it's almost done (except for this part). How to make it work? Thanks!

Answer

Reyna Lagunas picture Reyna Lagunas · May 18, 2017

Try:

curl -X POST -H "Content-type: application/json" --data "{\"text\": \"Pull requests has been deployed to\", \"username\": \"Staging Server\"}" http://requestb.in/ovehwtov

Obviously with the right data.

If you're on Windows you have to use "\".