Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get request body as string in Django

Tags:

python

django

I'm sending a POST request with JSON body to a Django server (fairly standard). On the server I need to decode this using json.loads().

The problem is how do I get the body of the request in a string format?

I have the following code currently:

body_data = {} if request.META.get('CONTENT_TYPE', '').lower() == 'application/json' and len(request.body) > 0:     try:         body_data = json.loads(request.body)     except Exception as e:         return HttpResponseBadRequest(json.dumps({'error': 'Invalid request: {0}'.format(str(e))}), content_type="application/json") 

However, this gives an error the JSON object must be str, not 'bytes'.

How do I retrieve the body of the request as a string, with the correct encoding applied?

like image 768
zelanix Avatar asked Apr 08 '15 12:04

zelanix


People also ask

What is request body in Django?

Request bodies are typically used with “create” and “update” operations (POST, PUT, PATCH). For example, when creating a resource using POST or PUT, the request body usually contains the representation of the resource to be created. To declare a request body, you need to use Django Ninja Schema .

What is HttpRequest in Django?

When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function. Each view is responsible for returning an HttpResponse object.

What is JsonResponse in Django?

class JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, **kwargs) An HttpResponse subclass that helps to create a JSON-encoded response. It inherits most behavior from its superclass with some differences: Its default Content-Type header is set to application/json .

What is request method == POST in Django?

The result of request. method == "POST" is a boolean value - True if the current request from a user was performed using the HTTP "POST" method, of False otherwise (usually that means HTTP "GET", but there are also other methods).


1 Answers

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

body_unicode = request.body.decode('utf-8') body_data = json.loads(body_unicode) 

In Python 2, json.loads will accept a unicode string or a byte sting, so the decode step is not necessary.

When decoding the string, I think you're safe to assume 'utf-8' - I can't find a definitive source for this, but see the quote below from the jQuery docs:

Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding.

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).

like image 177
Alasdair Avatar answered Sep 21 '22 22:09

Alasdair