1

I'm using curl to post login information to a json API. I'm confused because the following two calls, which I thought were equivalent, are giving different results:

curl -v -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"username":"test@test.com","password":"secret"}' http://api.mysite.com/Services/v1/login.svc/login

This call results in a 400 Bad Request. However, if I put the login json into a file called "login.json", and change the call to:

curl -v -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d @login.json http://api.mysite.com/Services/v1/login.svc/login

Then the call works fine, gives a 200 results, and returns the expected json response.

Why is the first call not working?

Thanks!

Update for Rusty

I tried the following, both with and without single quotes surrounding the JSON:

curl -v -H "Accept: application/json" -H "Content-Type: application/json" --data-urlencode '{username:test@example.com,password:11111111}' "https://api.example.com/login"

But the data is not sent to the server. From curl's output:

Warning: Couldn't read data from file
Warning: "{username:test@example.com,password:11111111}", this makes an empty
Warning: POST.

and while the request did get a 200 OK response, the login, which works when I use the file method, still failed, presumably because the JSON is not being sent.

Jonah
  • 15,806
  • 22
  • 87
  • 161
  • [Perhaps the POST data is mangled by the shell in the first example?](http://stackoverflow.com/a/1284087/7724) [Either way, if you can verify what's actually sent (using a local HTTP proxy or network sniffer perhaps) you can easily tell the difference between the two calls.](http://stackoverflow.com/a/3353174/7724) – bzlm Jul 25 '12 at 07:20
  • I used the curl --trace-ascii option to view the data that was being sent. It is not being mangled, so I don't think that's the problem – Jonah Jul 26 '12 at 08:01
  • please show the two different trace files. – bzlm Jul 26 '12 at 13:49

1 Answers1

0

Don't use -d,try to use --data-urlencode.

Because you may save the file in UTF8,so it urlencode your data automatically.

BTW,no need to use -X POST,if -Gis not used,POST is the default.

Hope that helps.

Jacob
  • 619
  • 1
  • 5
  • 17
  • please see me update to the OP, I tried but it didn't work. If I'm doing it wrong, please paste the correct curl cmd. – Jonah Aug 16 '12 at 13:32
  • @Jonah:I'm sorry,but the updated command seems right to me.Sorry can't help you. – Jacob Aug 17 '12 at 01:18