0

I'm using Django Rest Framework to build an API and I'm trying to redirect my endpoint to download files from AWS S3. It's working but in Chrome I'm getting the following warning:

Resource interpreted as Document but transferred with MIME type application/force-download

This is my get method from my view:

def get(self, request, pk, format=None):
    file_item = self.get_object(pk)
    serializer = FileSerializer(file_item)

    response = redirect(serializer.data['file_url'])
    return response

As you can see, I have passed content type, why does it Chrome still throw a warning? I need to be able to download files in various formats e.g. pdf, jpg etc.

Update: I have specified the mime type correctly and when I print the response object, this is what I get. Is it the "test/html" header that is causing issues? How do I remove it?

<HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="https://a.com/xxxx/x.pdf&response-content-type=application/pdf&response-content-disposition=attachment%3Bfilename%3D%22xxx.pdf%22">
kbsol
  • 402
  • 3
  • 17
  • Possible duplicate of [Utility of HTTP header "Content-Type: application/force-download" for mobile?](https://stackoverflow.com/questions/10615797/utility-of-http-header-content-type-application-force-download-for-mobile) – Jonah Bishop Nov 24 '18 at 12:50
  • Thanks for the insight. Note however that it returns the same warning even if I do not specify the content_type. In fact I only added it after seeing that as a recommendation somewhere else. – kbsol Nov 24 '18 at 13:32
  • I believe the root issue is that you _need_ to specify the content type, and it needs to match the actual file being processed. So, if you're sending a PDF, the content type needs to indicate that the file is a PDF, etc. – Jonah Bishop Nov 24 '18 at 13:51
  • Thanks Jonah for your comments, I have updated the code above, still having issues. – kbsol Nov 24 '18 at 14:52

2 Answers2

0

I see now that you are returning a redirect which is incorrect. Return instead an HttpResponse object:

response = HttpResponse(data, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
return response

data should be the file data, and filename the name of the file.

See this question for more.

Jonah Bishop
  • 11,619
  • 4
  • 38
  • 68
0

I have figured it out. The problem is that I'm passing headers in the "file_url" instead of the actual response object.

kbsol
  • 402
  • 3
  • 17