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 call decode
to decode the request.body
binary string into a JSON string.
Then we call json.loads
with the returned string to convert it to a dict.
For instance, we write
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
content = body['content']
We call request.body.decode
with the string encoding to decode to to decode the request.body
binary string.
Then we call json.loads
with the body_unicode
JSON string to return a dict from the JSON string.
Conclusion
To parse request.body from POST in Python Django, we can call decode
to decode the request.body
binary string into a JSON string.
Then we call json.loads
with the returned string to convert it to a dict.