Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django REST framework request data

Im using DJango REST framework to write my web service layer in which I want to read request payload from the request (POST). I tried the following code but I get empty set

@api_view(['POST'])
def login(request):
    print request.POST

Content Type is JSON. I tried to pass data from REST Client Tool. Still am able to read header values but only Payload is not coming out.

like image 434
syv Avatar asked Oct 28 '13 18:10

syv


People also ask

What is request in Django REST framework?

Django REST framework introduces a Request object that extends the regular HttpRequest, this new object type has request. data to access JSON data for 'POST', 'PUT' and 'PATCH' requests. However, I can get the same data by accessing request. body parameter which was part of original Django HttpRequest type object.

How do I import a CSV file into Django REST framework?

With all that done, you can go ahead and start your django server, navigate to the url you added which may look like this “http://127.0.0.1:8000/upload/”. Django Rest will display a page for you to upload your file, go ahead and upload a csv file using the sample data above and check your database to confirm it works.

What is difference between APIView and ViewSet?

APIView allow us to define functions that match standard HTTP methods like GET, POST, PUT, PATCH, etc. Viewsets allow us to define functions that match to common API object actions like : LIST, CREATE, RETRIEVE, UPDATE, etc.


1 Answers

You should use request.DATA instead of request.POST to retrieve json data.

request.DATA has been deprecated in favor of request.data since version 3.0, and has been fully removed as of version 3.2.

like image 106
mariodev Avatar answered Sep 28 '22 18:09

mariodev