1
votes

I am currently stuck with POST requests in Django. I am trying to send a POST request from an external applications such as smartphones or Postman (not forms) to the rest framework. Get requests work just fine.

I went through many posts but couldn't resolve my issue. I tried to use request.body but always get an empty response. I used print(response.body) to print the output to the console and only get b'' back.

class anyClass(APIView):

    def post(self, request):
        print(request.body)
        return Response({"id": 'anyClass',
                     "data": '1234',
                     })

How would I get the data from my request?

My post request sent with Postman: http://127.0.0.1:8000/test/v2/Api/anyClass?qrcode=100023&date=2018-11-27&time=08:00:00&value_1=17

1
Your POST request should have its data in the body, not in the query string.Klaus D.

1 Answers

10
votes

You can get the response in request.data:

class anyClass(APIView):

    def post(self, request):
        print(request.data)
        return Response({"id": 'anyClass',
                     "data": '1234',
               })

Please see the documentation for details.

Update

I think you are making wrong kind of usage of postman. Please see the screen shot regarding how to use it:

Postman Usage