{ "detail": "JSON parse error - Expecting value: line 1 column 1 (char 0)" }

18,532

Solution 1

I actually had the same error, you need to use JSON.stringify(data) like the following in case you are using ajax :

datato= {
    "id" : 3,
    "title" : "level up",
    "author" : "jason rock"

}

$.ajax({
    method:'POST',
    url:"/home/api",
    data : JSON.stringify(datato),

})

@SeriForte 's answer pointed me in the right way

Solution 2

In my case, i was not sending a correct json in the data. So please check that.

Solution 3

I have solved this problem,I can access data now

I changed my older code

print(request.data)
data = JSONParser().parse(request)

this will get an error. If I code like below:

print(request)
data = JSONParser().parse(request)

Then I can access data in the dictionary.

So, I did not know why but the issue is fixed

Share:
18,532

Related videos on Youtube

Michael Porter
Author by

Michael Porter

Updated on June 04, 2022

Comments

  • Michael Porter
    Michael Porter almost 2 years

    I am using django-restframework, I use postman POST json data to my project but I got the error like tittle, I have set raw and application/json here is the code from postman.

    POST /account/post/reply/ HTTP/1.1
    Host: localhost:8000
    Content-Type: application/json
    Cache-Control: no-cache
    Postman-Token: a0c7bd93-631e-4c7a-8106-87f018eaf7da
    
    {
        "user": "michael",
        "userid": "1",
        "ihelpid": 6,
        "tittle": "6",
        "info": "6",
        "label": "3",
        "tel": "dxy970525",
        "picture1": null,
        "picture2": null
    }
    

    my code is really easy only like :

    from rest_framework.parsers import JSONParser,ParseError
    
    class ReplyViewSet(viewsets.ModelViewSet):
        """
        This viewset automatically provides `list` and `detail` actions.
        """
    
        pagination_class=PageNumberPagination
        queryset = Forum_reply.objects.all()
        serializer_class = ReplySerializer
    
        #filter
        filter_backends = (DjangoFilterBackend, )
        filter_fields = ['postID',]
        def create(self, request, *args, **kwargs):
            print(request.data)
            data = JSONParser().parse(request)
            return HttpResponse("ok")
    

    After I use viewsets,this error occur,I have print it on shell but it is no problem

    • rdas
      rdas about 5 years
      Hey, can you debug by checking out what the variable request is? JSONParser should work if request is a JSON string. Seems like it's not so in this case.
    • mariodev
      mariodev about 5 years
      You should instead use parser_classes = (JSONParser,) (google it). This will automatically parse request data for you, so you can access it in request.data
    • Michael Porter
      Michael Porter about 5 years
      If i print request,I get <rest_framework.request.Request object at 0x7f03c01a1470>
    • Michael Porter
      Michael Porter about 5 years
      Thinks for telling me print request but not request.data..
    • abduljalil
      abduljalil over 2 years
      please check your function where you are sending requests. I was facing same issue, i resolved it by changing this.http.delete(this.APIUrl + '/website', val) to this.http.delete(this.APIUrl + '/website',{body:val}).