简体   繁体   中英

Django Rest Framework: How to send data to request.data

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

  1. curl: curl --data param1=val1&param2=val2 url
  2. djangos browser interface with post (using formatted url and the post option
  3. 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&param2=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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM