6

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.

3
  • 1
    Can you provide more detail as to how you are sending your POST request? Have you tried debugging to inspect request? Commented Oct 28, 2013 at 18:41
  • curl -X POST -H "Content-Type: application/json" -d ' { "name": "siva", }' 127.0.0.1:8000/authentication/login when I print.POST I got "<QueryDict: {}>" Commented Oct 28, 2013 at 18:47
  • You may also be running into CSRF issues, are you exempting the view from using csrf? Commented Oct 28, 2013 at 19:00

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you .. The problem was with Cache. Earlier I tried with request.DATA but it didnt succeeded. Now it works.
request.DATA has been deprecated in favor of request.data
request.DATA print all the vairable but In want one vairable value hwo can i do this?
You should use request.data['username'] to get the value of a particular variable. Here you can replace any variable as you like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.