I have a simple API that downloads a file in the system. It works perfectly in the local server, but when I deploy the same code to production, it gives me a 500 server error. If I try with another id that doesn't exist, it will say the object is not found.
My code is as follows.
def get_permissions(self):
"""
Instantiates and returns the list of permissions that this view requires.
"""
if self.request.query_params.get("download") == "file":
return []
return [permission() for permission in self.permission_classes]
def get(self, request, *args, **kwargs):
if request.query_params.get("download") == "file":
return self.download_file(request)
/....other codes........./
def download_file(self, request, *args, **kwargs):
instance = self.get_object()
file_handle = instance.file.path
document = open(file_handle, "rb")
response = HttpResponse(FileWrapper(document), content_type="")
response["Content-Disposition"] = (
'attachment; filename="%s"' % instance.file_name
)
return response
My endpoint URL is this:
{{prod}}api/v1/example/notes/12/?download=file
When I call this api, putting local in place of prod, it works and I get a file downloaded, but not in production. Is it something to do with file being closed before getting a response??