1

I have a Django app that needs to interact with a database of medical images. They have a RESTful API that returns a stream of bytes, which I am writing to a HttpStreamingResponse. This is working, but the issue is that it is very slow. Most files I am downloading are around 100mb, and it usually takes around 15-20 seconds before the download even begins. Does anyone have insight into how to speed up this process and start the download faster?

Here is my code:

# Make api call
response = requests.get(url, cookies=dict(JSESSIONID=self.session_id))
# write bytes to Http Response 
http = StreamingHttpResponse(io.BytesIO(response.content), content_type='application/zip')

http['Content-Disposition'] = 'attachment; filename="%s.zip"' % patient_id
return http

1 Answer 1

2

You are downloading the full response to your server before passing on the information.

You should just forward the response from the API call using the following:

res = FROM API CALL
response = HttpResponse(ContentFile(res.content), 'application/zip')
response['Content-Disposition'] = 'attachment; filename={}.zip'.format(patient_id)
response['Content-Length'] = res.headers.get('Content-Length')
response['Content-Transfer-Encoding'] = res.headers.get('Content-Transfer-Encoding')
response['Content-Type'] = res.headers.get('Content-Type')
return response

Make sure you copy over any important headers.


Edit: Since this is the only proposed solution, I'm editing to include the solution from John's comment in a more readable format:

# Make api call 
response = requests.get(url, cookies=dict(JSESSIONID=self.session_id), stream=True) 
# write bytes to Http Response 
http = StreamingHttpResponse(response.iter_content(8096), content_type='application/zip') 
http['Content-Disposition'] = 'attachment; filename="%s.zip"' % patient_id 
return http
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Kevin, I had the same issue with your block of code but just solved it by adding stream=True to response. Here is the solution for others with the same issue: # Make api call response = requests.get(url, cookies=dict(JSESSIONID=self.session_id), stream=True) # write bytes to Http Response http = StreamingHttpResponse(response.iter_content(8096), content_type='application/zip') http['Content-Disposition'] = 'attachment; filename="%s.zip"' % patient_id return http

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.