Akash Deshpande Akash Deshpande - 8 months ago 65
JSON Question

Curl Post Json data not being read in Python Django

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 ?


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/"

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.


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


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