Json parse error using POST in django rest api

I am trying to implement a simple get / post api through the Django REST framework

views.py

 class cuser(APIView): def post(self, request): stream = BytesIO(request.DATA) json = JSONParser().parse(stream) return Response() 

urls.py

 from django.conf.urls import patterns, url from app import views urlpatterns = patterns('', url(r'^challenges/',views.getall.as_view() ), url(r'^cuser/' , views.cuser.as_view() ), ) 

I'm trying to POST add json to /api/cuser/ (api is the namespace in my urls.py project), JSON

 { "username" : "abhishek", "email" : " john@doe.com ", "password" : "secretpass" } 

I tried from both pages the view API and httpie (a tool created by python that looks like curl)

httpie command

 http --json POST http://localhost:58601/api/cuser/ username=abhishek email=john@doe.com password=secretpass 

but I get a JSON parsing error:

 JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1) 

Whole Debug message using --verbose --debug

  POST /api/cuser/ HTTP/1.1 Content-Length: 75 Accept-Encoding: gzip, deflate Host: localhost:55392 Accept: application/json User-Agent: HTTPie/0.8.0 Connection: keep-alive Content-Type: application/json; charset=utf-8 {"username": "abhishek", "email": " john@doe.com ", "password": "aaezaakmi1"} HTTP/1.0 400 BAD REQUEST Date: Sat, 24 Jan 2015 09:40:03 GMT Server: WSGIServer/0.1 Python/2.7.9 Vary: Accept, Cookie Content-Type: application/json Allow: POST, OPTIONS {"detail":"JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"} 
+7
source share
3 answers

The problem you are facing is that your request is already parsed and you are trying to parse it a second time.

From "How the parser is defined"

The set of valid parsers to represent is always defined as a list of classes. When accessing request.data , the REST structure will look at the Content-Type header in the incoming request and determine which parser to use to parse the contents of the request.

In your code, you get access to request.data , which is the equivalent of 2.4.x request.data . This way your request is processed as soon as you call it, and request.data actually returns the dictionary that you expected for parsing.

 json = request.DATA 

- this is really all you need to parse incoming JSON data. You really passed the Python dictionary to json.loads , which didn't seem to be able to parse it, and that is why you got your error.

+8
source

I came to this post via Google for "detail": "JSON parse error - Waiting for a property name enclosed in double quotes:". Turns out you CANNOT have a trailing comma in JSON, as I'm used to having in Python. Therefore, if you get this error, you may need to modify the entry as follows:

 { "username" : "abhishek", "email" : " john@doe.com ", "password" : "secretpass", } 

:

 { "username" : "abhishek", "email" : " john@doe.com ", "password" : "secretpass" } 

Note the remote comma after the last property of the JSON object.

+1
source

Basically, whenever you try to make a mail request with lib requests, this library also contains a json argument, which is ignored when the data argument is set to files or data. So basically when the json argument is given with json data. Headers are set as Content-Type: application/json . The json argument basically encodes the data in json format. So in DRF, in particular, you can parse json data. Else in case of only data argument is treated as encoded in the form

requests.post(url, json={"key1":"value1"})

here you can find here request.post sophisticated publishing methods

0
source

Source: https://habr.com/ru/post/981461/


All Articles