0

I am trying to use CURL to put JSON data in my request by using this syntax:

curl -X POST -H "Content-Type: application/json" -d '{"firstName":"First", "lastName":"last", "email":"[email protected]", "username":"user1", "password":"pass1"}' 127.0.0.1:3001/users

now the CMD is showing this error: as you can see

Please help!

2
  • Windows doesn't treat ' the same way a Unix shell would. Commented Jun 10, 2018 at 8:50
  • So what should be done to resolve it? @user2357112 Commented Jun 10, 2018 at 9:01

3 Answers 3

1

Well I used:

curl -X POST -H "Content-Type: application/json" -d "{\"firstName\": \"First\", \"lastName\": \"last\", \"email\": \"[email protected]\", \"username\": \"user1\", \"password\": \"pass1\"}" 127.0.0.1:3001/users

And it worked! Shout out to Ross Kinard for helping with a temporary solution so that this question now specifies both way in which we can post using CURL in cmd.

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

Comments

1

I'd let batch do the escaping by replacing all " with \"

set "json={"firstName":"First", "lastName":"last", "email":"[email protected]", "username":"user1", "password":"pass1"}"
set "json=%json:"=\"%"
echo curl -X POST -H "Content-Type: application/json" -d "%json%" 127.0.0.1:3001/users

Remove the echo in front of curl before using.


  • If running a batch in powershell curl without extension will use the alias to Invoke-Webrequest instead of curl.exe
  • I Windows 10 Version 1803 is curl.exe included (C:\Windows\system32\curl.exe V7.55.1.0)
  • If you need a special version of curl the location C:\Windows\system32\ is presumably before in the environment path, so include the path to your curl then.

1 Comment

Well this is a nice brilliant solution! Thanks.
0

I had a similar problem earlier, and my solution was to save the post data in a .json file and call it in the request.

curl -v -X POST https://someSite.com/randrand \
  -H 'Content-Type: application/json' \
  -H 'User-Agent: (iPhone; iOS 7.1; Scale/2.00)' \
  --data @data.json

5 Comments

where the file must be saved? like in which directory? Should that be same as where cmd is currently?
I saved it in the same directory I was running the terminal in, in my case, my desktop.
Thanks! It solved the problem for now but what if I don't wanna use this? @Ross Kinard
I tried for a long time to find the right syntax for the data section, but I was not able to with the elements I was posting using the terminal on a Mac. If you find a better answer, let me know.
Yeah I'll let you know certainly if I find something out.

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.