Json parse error using POST in django rest api

The problem that you are running into is that your request is already being parsed, and you are trying to parse it a second time.

From "How the parser is determined"

The set of valid parsers for a view is always defined as a list of classes. When request.data is accessed, REST framework will examine the Content-Type header on the incoming request, and determine which parser to use to parse the request content.

In your code you are accessing request.DATA, which is the 2.4.x equaivalent of request.data. So your request is being parsed as soon as you call that, and request.DATA is actually returning the dictionary that you were expecting to parse.

json = request.DATA

is really all you need to parse the incoming JSON data. You were really passing a Python dictionary into json.loads, which does not appear to be able to parse it, and that is why you were getting your error.


I arrived at this post via Google for

"detail": "JSON parse error - Expecting property name enclosed in double-quotes": Turns out you CANNOT have a trailing comma in JSON.

So if you are getting this error you may need to change a post like this:

{
    "username" : "abhishek",
    "email" : "[email protected]",
    "password" : "secretpass",
}

to this:

{
    "username" : "abhishek",
    "email" : "[email protected]",
    "password" : "secretpass"
}

Note the removed comma after the last property in the JSON object.