0

there is something I can't well understand.

When I'm using curl against a freshly raw ASP.NET app with JSON using this command:

curl -X POST -H "Content-Type: application/json" -X POST -d '{"username":"abc","password":"abc"}' https://localhost:5001/api/values --insecure -v

I have a failed response (bad request 400) with the following error:

{"":["Unexpected character encountered while parsing value: {. Path '', line 1, position 1."]}

However... If I copy the json data in a file and call the CURL command as this:

curl -X POST -H "Content-Type: application/json" -X POST -d @user.json https://localhost:5001/api/values --insecure -v

It works just fine.

====> Why/How the hell?

Thanks

1
  • What did you send? You don't really know. Obviously, curl did something different in those two cases. Use a debugging proxy like Fiddler to see what was really sent to the service. Commented Feb 8, 2019 at 14:18

2 Answers 2

1

this happens to me all the time and I always forget!

Simple fix, the inline data for curl doesn't auto escape the double quotes, so all you have to do is :

curl -X POST -H "Content-Type: application/json" -X POST -d "{\"username\":\"abc\",\"password\":\"abc\"}" https://localhost:5001/api/values --insecure -v
Sign up to request clarification or add additional context in comments.

Comments

1

Long story short, the curl version for Windows doesn't behave the same as the other ones. Either use double quotes or use the Linux curl version from the Windows Subsystem for Linux.

Long explanation

If different curl calls result in different responses, it's clear that curl does something different, not ASP.NET. Using a debugging proxy like Fiddler shows that the first call sends :

POST http://localhost:5001/api/values HTTP/1.1
Host: localhost:5001
User-Agent: curl/7.55.1
Accept: */*
Proxy-Connection: Keep-Alive
Content-Type: application/json
Content-Length: 29

'{username:abc,password:abc}'

Replacing single and double quotes with :

curl -H "Content-Type: application/json" -X POST -d "{'username':'abc','password':'abc'}" http://localhost:5001/api/values --insecure -v -x 127.0.0.1:8888

Sends :

POST http://localhost:5001/api/values HTTP/1.1
Host: localhost:5001
User-Agent: curl/7.55.1
Accept: */*
Proxy-Connection: Keep-Alive
Content-Type: application/json
Content-Length: 35

{'username':'abc','password':'abc'}

Usin Windows Subsystem for Linux

All this can be avoided by using the Linux binaries through the Windows Subsystem for Linux. That's a full Linux user space environment. In this case both requests send valid JSON :

{"username":"abc","password":"abc"}

and

{'username':'abc','password':'abc'}

4 Comments

I still get the same error :(, but if I wrap everything with double quotes, it works: curl -X POST -H "Content-Type: application/json" -X POST --data "'{"username":"abc","password":"abc"}'" localhost:5000/api/values --insecure -v
@MichaelManuelVandycke check the update. You can avoid this if you use WSL and the Linux version of curl. I tried it with Ubuntu, there are other distros as well in the Windows App Store, eg SUSE, Kali, Debian
I use WSL, if I do my curl like this: curl -X POST -H "Content-Type: application/json" -X POST --data {"username":"abc","password":"abc"} localhost:5000/api/values --insecure -v, I get {"":["Unexpected character encountered while parsing value: u. Path '', line 1, position 1."]}.
However it works with this: curl -X POST -H "Content-Type: application/json" -X POST --data "'{"username":"abc","password":"abc"}'" localhost:5000/api/values --insecure -v

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.