How to read JSON data in an HTTP POST request in Django
1 min readSep 26, 2020
If you are working with Django, you might have faced this issue. We use request.GET
to get the data in a GET request. But if you try to do the same with request.POST
it would return an empty <QueryDict: {}>
.
Where did the POST data go? Why is it empty? It is because request.POST
contains only the data sent from an HTML <form>
element. So if need to read JSON data from the POST request you need to use json.loads(request.body)
.
request.body
gives you a raw HTTP request body as a bytestring. You can convert into a dictionary using json.loads()
.