4

I have already searched for similar questions on StackOverflow but could not find the solution that would work for me. I have the following code snippet:

def post_request(request):

if request.method == "POST":
    access_token = AccessToken.objects.get(token = request.POST.get("access_token"), expires__gt = timezone.now())

When I make the HTTP request to this function, I get the following error: AccessToken matching query does not exist. , I started debugging the app.

The problem is in request.POST.get("access_token")

request.POST = <QueryDict: {}> . I have no data in there

I am making a POST request in POSTMAN, where I added the data to body -> form-data. I also tried sending data through body->raw->json but anyway the request.POST does not contain anything.

enter image description here

What is the cause that I have no Post data? How can I get the POST data?

Thank you!

7
  • can you post your template code here ?
    – varnit
    Jan 30, 2018 at 18:31
  • what do you mean by template code? Jan 30, 2018 at 18:33
  • view template from which the post request is coming
    – varnit
    Jan 30, 2018 at 18:33
  • I am using postman. I will upload now the screenshot Jan 30, 2018 at 18:34
  • can you please try this request.POST.get("access_token", "") and just try to print it
    – varnit
    Jan 30, 2018 at 18:42

1 Answer 1

2

Try using x-www-form-urlencoded instead of form data. x-www-form-urlencoded is the default form encoding for HTML forms.

Django does support enctype="multipart/form-data", which can be used for uploading files. So I'm afraid I can't explain why your request from Postman didn't work when you selected form data. Perhaps your request had an incorrect header.

3
  • it is very strange but yesterday it worked, but today I can not see again anything in the request.POST Jan 31, 2018 at 13:09
  • it does not matter how I send the request (form-data or x-www-form-url-encoded) Jan 31, 2018 at 13:10
  • 1
    the problem seems to be the way I run the project. When I run the project using the configuration of Visual Studio Code, I get always null post. When the project is ran through terminal it works. Very strange Feb 1, 2018 at 12:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.