How to parse request body from POST in Django

0 votes

For some reason I cannot figure out why Django isn't handling my request.body content correctly.

It is being sent in JSON format, and looking at the Network tab in Dev Tools shows this as the request payload:

{creator: "creatorname", content: "postcontent", date: "05/08/2020"}

which is exactly how I want it to be sent to my API.

In Django I have a view that accepts this request as a parameter and just for my testing purposes, should print request.body["content"] to the console.

Of course, nothing is being printed out, but when I print request.body I get this:

b'{"creator":"creatorname","content":"postcontent","date":"05/08/2020"}'

so I know that I do have a body being sent.

I've tried using json = json.loads(request.body) to no avail either. Printing json after setting that variable also returns nothing.

Aug 5, 2020 in Python by kartik
• 37,520 points
28,527 views

1 answer to this question.

0 votes

Hello @kartik,

In Python 3.0 to Python 3.5.x, json.loads() will only accept a unicode string, so you must decode request.body (which is a byte string) before passing it to json.loads().

body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
content = body['content']

In Python 3.6, json.loads() accepts bytes or bytearrays. Therefore you shouldn't need to decode request.body (assuming it's encoded in UTF-8, UTF-16 or UTF-32).

Hope it helps!!

Thank you!!

answered Aug 5, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer

How to order_by a JSON from serializers.py file in django rest framework?

Hello @kartik, There's an easy way, just override ...READ MORE

answered Jul 2, 2020 in Python by Niroj
• 82,840 points
5,871 views
0 votes
1 answer
0 votes
1 answer

How to clear a database from the CLI with manage.py in Django?

Hello @kartik, To drop the database and run syncdb again. ...READ MORE

answered Aug 7, 2020 in Python by Niroj
• 82,840 points
2,774 views
0 votes
1 answer

How to redirect to named url pattern directly from urls.py in django?

Hello @kartik, You can try this: from django.views.generic import ...READ MORE

answered Aug 12, 2020 in Python by Niroj
• 82,840 points
2,604 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,451 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,564 views
0 votes
1 answer

How to output text from database with line breaks in a django template?

Hello @kartik, Use linebreaks or linebreaksbr filter: {{ text|linebreaks }} Or surround the text ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,840 points
13,493 views
0 votes
1 answer

How to get value from form field in django framework?

Hello @kartik, Using a form in a view can ...READ MORE

answered Jun 30, 2020 in Python by Niroj
• 82,840 points
9,134 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP