Kate Cebotari
Kate Cebotari

Reputation: 309

Django request.post no data available

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!

Upvotes: 4

Views: 2497

Answers (1)

Alasdair
Alasdair

Reputation: 309099

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.

Upvotes: 2

Related Questions