5

Am using curl exe in windows, to communicate with my Django backend.

Following is the command am using.

curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data "{\"uid\":12,\"token\":\"asdert\"}" http://localhost:8000/restapi/v1/foo/

Now this give the data in wrong format. i.e. in the view the post is showing this data print request.POST

{"{\"uid\":12,\"access_token\":\"asdert\"}": [""]}

What is the correct way to post json data ?

Edit:

I have tried several other methods for e.g. I am trying to communiate with my rest api using http://slumber.in/.

Even here am getting the same result as above.

import slumber
api = slumber.API("http://localhost/restapi/v1/"
api.foo.post({"uid":"100"})

Excerpts from the view print request.POST

 {u'{"uid": "100"}': [u'']}

P.S. - curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data "uid=12&token=asdert" http://localhost:8000/restapi/v1/foo/

This works. But this is not Json format.

1
  • No if you print the request.POST in the view it gives a dicitionary with key as the data above and a an empty value. Commented Oct 21, 2012 at 9:54

1 Answer 1

9

I tried your command with http://httpbin.org/post and it worked fine.

Now, your problem is that you should access the incoming JSON data from

request.raw_post_data

instead of request.POST.

(Or if you are using Django 1.4+, use request.body instead as request.raw_post_data is being deprecated)


Detailed code should be something like this:

import json

if request.method == "POST":
    data = json.loads(request.raw_post_data)
    print data
Sign up to request clarification or add additional context in comments.

7 Comments

It is giving an error that the object is not Json. i.e. No Json object could be decoded
@AkashDeshpande can you try print request.raw_post_data?
'{uid:1234,access_token:aaa}'
@AkashDeshpande Interesting, what if you simply use curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data '{"uid":12,"token":"asdert"}' http://localhost:8000/restapi/v1/foo/ instead without the windows-specific hack?
Same as above '{uid:100,access_token:AAA}' I used this command though curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data '{"uid":100,"access_token":"AAA"}' localhost:8000/restapi/v1/icrave
|

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.