Django unit tests: How to populate request.raw_post_data

If you are creating unit tests with Django, you might need to send raw data with the Client object. In the example below, we are testing how our view reacts to a raw POST string sent by another API. The string in question is myrawpoststring.

All you need to do is set the data attribute to your string, then set the content_type attribute to application/octet-stream. The default content type, multipart/form-data, expects a dictionary. Any other value will accept raw strings, including made-up content types. Semantically, the application/octet-stream content type is the correct one to use.

class MyTestCase(TestCase)
    ...

    def test_partial_refund(self):

        #Send a request to /api/sync/23/ with a raw POST string
        response = self.client.post('/api/sync/23/', data='myrawpoststring', content_type='application/octet-stream')
        ...

The .put(), .patch() and .delete() equivalents already set the content_type to ‘application/octet-stream’ by default, so you will only need to specify a string.

In your view, you will now be able to access your string in the request object:

def my_simple_view(self, request):
    post = request.raw_post_data # post will equal 'myrawpoststring'
    ...

One comment on “Django unit tests: How to populate request.raw_post_data

  1. Thank you, but I found it better to use content_type=’application/x-www-form-urlencoded’ because then Django will decode the raw_post_data so that its contents are also available via the request.POST.

Leave a Reply

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

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax