Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Content-disposition header for Django FileUpload

I defined an API endpoint which accepts a file (e.g. using Django REST Framework). In Django, the content disposition header can be used when inspecting the response.

https://docs.djangoproject.com/en/1.11/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

Now, if we want to set the header when testing the endpoint, how do I include this header using REST-Framework's APITestCase?

What I tried so far is, but it does not seem to accept the headers.

class TestSaleViews(APITestCase):
    def test_sale_detail_view(self):
        f = create_named_temporary_file()
        files = {'archive': f}
        basename = os.path.basename(f.name)
        headers = {
            'content-disposition': 'attachment; filename={}'.format(basename),
        }
        response = self.client.post(url, files, format='multipart', **headers)
like image 280
markus-hinsche Avatar asked May 29 '17 14:05

markus-hinsche


1 Answers

Found the answer!

Django has a fixed keyword for this header in its FileUploadParser. It is: HTTP_CONTENT_DISPOSITION

So I needed to replace it et voila: worked!

headers = {
  'HTTP_CONTENT_DISPOSITION': 'attachment; filename={}'.format(basename),
}

https://github.com/encode/django-rest-framework/blob/master/rest_framework/parsers.py#L206

like image 62
markus-hinsche Avatar answered Nov 13 '22 05:11

markus-hinsche