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.
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.
Related
Im building a simple api in Django Rest Framework using class based views and the documentation mentions request.data (https://www.django-rest-framework.org/tutorial/2-requests-and-responses/)
from rest_framework.views import APIView
class myView(APIView):
def post(self, request):
print(request.data)
When I try to send post requests either with
curl: curl --data param1=val1¶m2=val2 url
djangos browser interface with post (using formatted url and the post option
Advanced Rest Client (chrome add on)
The data of all three appears to end up in request.query_params.
When I try to print request.data, however I get an empty response {}
I could just use request.query_params, Im just curious as to how one would get data to go to request.data since it is talked about in the documentation
edit: so curl --data param1=val1¶m2=val2 -X POST url sends information to request.data (now if I say request.query_params if get the empty dict {} but request.data contains an object that is the dict query_params.)
curl request doesn't have the -X and POST added.
Add these
I use Django Channels to retrieve the data from Django Restful (DRF) serializer (I use channels because database is large and if I call for the data directly it results in a server timeout).
What I struggle with (perhaps I don't understand something about how DRF works), is how to get the html representation of Browsable API. So basically what I need to do is to send back as html a response based on BrowsableAPIRenderer when a person connects via WebSocket:
def connect(self, message, **kwargs):
myobj = MyObj.objects.filter(code=self.obj_code)
serializer = MyObjSerializer(myobj, many=True)
self.send(Response(serializer.data))
But that results in an error Response is not JSON serializable.
I'm using django 1.8 rest api as below.
#api_view(['GET', 'POST'])
#authentication_classes((TokenAuthentication, SessionAuthentication, BasicAuthentication))
#permission_classes((IsAuthenticated,))
#staff_member_required
def api(request):
print request.data
The problem with this is that it retrieves all parameters as string so I have to convert numbers and boolean manually.
I tried using:
print json.loads(request.body)
but apparently django rest framework reads from the data stream which causes this error:
Error: RawPostDataException: You cannot access body after reading from request's data stream
I also tried
json.loads(request.stream.body)
because the documentation says stream should work.
Is there a way I can retrieve the request post data with the appropriate types?
Note that I'm using ajax to send the data with JSON.stringify.
You want to use serializers in DRF. They will allow you to convert model instances to native Python datatypes which can be translated into JSON and vice versa. You can also deserialize which will convert those JSON strings to complex types.
Read more about serializers here
Hi i am making an webserver , In which I have to hit some request from html page and return the response. The URL which is generated using html is
http://192.168.2.253:8080/searchSMS/?KPImsgId=0&circle=&subId=&startDate=DD-MM-YYYY&endDate=DD-MM-YYYY&Username=ashish
but in the server side I am not able to see the request data. I am using
q = QueryDict(request.body) but it is showing <QueryDict: {}>
How to find the all the parameters coming in request.
In your case you send the data in url so access the data through request.GET as follow:
username = request.GET.get('Username')
start_date = request.GET.get('startDate')
# ... the same for all the other parameter after the `?` marque.
In fact there is a difference between request data, request.body, request.GET and request.POST:
If you are sending POST request to django function view or class based view: you access the request data in request.body or request.POST.
If you are sending POST request to Django REST Framework: you access the data in request.data. You may also find in Internet request.DATA that correct but it's deprecated in the newer version of DRF in favor of request.data.
If you send parameter in the url like in you case, you access the data form request.GET as explained above.
Django request handler let you get POST parameters using request.DATA
def post(self, request):
request.DATA.
How could I get DELETE parameters?
I tried
def delete(self,request):
request.body
request.read()
Both request.body and request.read() just displays csrfmiddlewaretoken and _method parameters.
For both of the above example I am sending the parameters as application/json.
How could I get the delete request parameters?
I know this question is quite old, but for reference: I think you might want Request.QUERY_PARAMS. See the REST Framework docs for more info: http://django-rest-framework.org/api-guide/requests.html