title: Python: Download zip file from RestAPI
body: <p>Third party RestAPI provides server log file, at a moment we are downloading  log file using curl command like</p>
  
  <pre><code>curl -H "X-Auth-Token: XXXXXXXXXXXXXXXXXXXXXXXXXX" https://host_address/api/v3.0/admin/logs -o logs.zip
  </code></pre>
  
  <p>but I am trying to create simple dashboard using Flask/Python,</p>
  
  <p>Here is my Python/Flask code for the route:</p>
  
  <pre><code>@app.route('/download/server/logs')
  def download_log():
      import requests
      from flask import send_file
      res = requests.get('http://&lt;rest_api_host&gt;/v1.2/admin/logs', stream=True)
      return send_file(
          res.content,
          attachment_filename='console_log.zip',
          mimetype='application/zip'
      )
  </code></pre>
  
  <p>But when I hit that url from browser I get below error,</p>
  
  <pre><code>Traceback (most recent call last):
  ...
  ...
  ...
    File "/Users/admin/Documents/project/__init__.py", line 940, in download_console_logs
      res.content,
    File "/Users/admin/Documents/project/venv3/lib/python3.6/site-packages/requests/models.py", line 823, in content
      self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
    File "/Users/admin/Documents/project/venv3/lib/python3.6/site-packages/requests/models.py", line 745, in generate
      for chunk in self.raw.stream(chunk_size, decode_content=True):
    File "/Users/admin/Documents/project/venv3/lib/python3.6/site-packages/urllib3/response.py", line 436, in stream
      data = self.read(amt=amt, decode_content=decode_content)
    File "/Users/admin/Documents/project/venv3/lib/python3.6/site-packages/urllib3/response.py", line 384, in read
      data = self._fp.read(amt)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 449, in read
      n = self.readinto(b)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 497, in readinto
      self._close_conn()
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 403, in _close_conn
      fp.close()
  AttributeError: 'NoneType' object has no attribute 'close'
  </code></pre>
  
  <p>I put the breakpoint/debugger using PyCharm and able to see that <code>res.content</code> contains binary data in it, but I'm not able to figure it out what is wrong here.</p>
  
  <p>Here is the simple diagram which explains what I'm trying to do,
  <a href="https://i.stack.imgur.com/FICHi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FICHi.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE: ANSWER:</strong> I took below approach and it solves my problem in a very efficient way.</p>
  
  <pre><code>@app.route('/download/server/logs')
  def download_log():
      import requests
      from flask import Reponse
      res = requests.get('http://&lt;rest_api_host&gt;/v1.2/admin/logs', stream=True)
      return Response(
          res.iter_content(chunk_size=1024),
          direct_passthrough=True
      )
  </code></pre>