1

I have created a api which i am sending request by post but didn't get vairable in a view

def login_list(request):
    if request.method == 'POST':
        data = json.dumps(request.POST)
        print(data)
        serializer = LoginSerializer(data=request.data)
        #print(serializer)
        return JsonResponse({"message":'fdsafdsa'}) 

when i print data print(data) then out put is coming like this

{"{\"login\":122122,\"abvc\":\"544545\"}": ""}

and i calling this api like this in postman

Post http://localhost:8000/login/login/

{"login":122122,"abvc":"544545"}

I am not geting value with this

print(request.POST['login']);

how can i get value

6
  • "how can i assign login variable value in a variable?" what does that mean? Commented Mar 28, 2017 at 12:19
  • i want to print login variable value Commented Mar 28, 2017 at 12:21
  • request.POST['login'] this is not working Commented Mar 28, 2017 at 12:22
  • define not working Commented Mar 28, 2017 at 12:23
  • Why would you think it should work? Commented Mar 28, 2017 at 12:25

1 Answer 1

1

Try request.data instead of request.POST. JSON Content is sent in body, which is parsed by Django Rest Framework at runtime.

login_variable = request.data['login']

And ensure you have added 'JSONParser' in REST_FRAMEWORK settings.

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

6 Comments

Have you added JSONParser in your rest framework settings
MultiValueDictKeyError at /login/login/ "'login'"
{"detail":"Unsupported media type \"application/x-www-form-urlencoded\" in request."} after add rest framework settings JSONParser
Send 'Content-Type': 'application/json' headers in your request
Thanks @hspandher
|

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.