Categories
Python Answers

How to parse request.body from POST in Python Django?

Spread the love

Sometimes, we want to parse request.body from POST in Python Django.

In this article, we’ll look at how to parse request.body from POST in Python Django.

How to parse request.body from POST in Python Django?

To parse request.body from POST in Python Django, we can decode request.body into a string.

And then we can call json.loads on the decoded string.

For instance, we write

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

in a view function.

We call decode to decode request.body into a JSON string.

Then we call json.loads on the string into a dict.

Conclusion

To parse request.body from POST in Python Django, we can decode request.body into a string.

And then we can call json.loads on the decoded string.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *