9

I am trying to implement a simple GET/POST api via 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 am trying to POST some json to /api/cuser/ (api is namespace in my project's urls.py ) , the JSON

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

I tried from both Browseable API page and httpie ( A python made tool similar to curl)

httpie command

http --json POST http://localhost:58601/api/cuser/ username=abhishek [email protected] password=secretpass

but I am getting JSON parse 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": "[email protected]", "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)"}
5
  • Please show exactly how you are posting that JSON. Where from? Commented Jan 23, 2015 at 16:47
  • 1
    @DanielRoseman Tried both httpie and from browseable api page Commented Jan 23, 2015 at 16:51
  • Try adding --verbose --debug to the HTTPie command. That will show you the request that is being sent as well as some additional debug output. Commented Jan 23, 2015 at 23:38
  • @JakubRoztočil added debug output , although I can't make much from it Commented Jan 24, 2015 at 9:48
  • I think there is some bug on the server side. The request/JSON looks correct. You can also try to send the same request to httpbin.org and see how it gets interpreted: $ http --json httpbin.org/post username=abhishek [email protected] password=secretpass Commented Jan 24, 2015 at 11:09

3 Answers 3

14

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.

Sign up to request clarification or add additional context in comments.

Comments

12

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.

2 Comments

is the data is auto-rendered to json when sending back too?
If you use the custom response class, yes.
0

Basically, whenever you are trying to make a post request with requests lib, This library also contains json argument which is ignored in the case when data argument is set to files or data. So basically when json argument is set with json data. Headers are set asContent-Type: application/json. Json argument basically encodes data sends into a json format. So that at DRF particularly is able to parse json data. Else in case of only data argument it is been treated as form-encoded

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

you can find more here request.post complicated post methods

Comments

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.