title: Get request body as string in Django
body: <p>I'm sending a POST request with JSON body to a Django server (fairly standard). On the server I need to decode this using <code>json.loads()</code>.</p>
  
  <p>The problem is how do I get the body of the request in a string format?</p>
  
  <p>I have the following code currently:</p>
  
  <pre><code>body_data = {}
  if request.META.get('CONTENT_TYPE', '').lower() == 'application/json' and len(request.body) &gt; 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")
  </code></pre>
  
  <p>However, this gives an error <code>the JSON object must be str, not 'bytes'</code>.</p>
  
  <p>How do I retrieve the body of the request as a string, with the correct encoding applied?</p>