0

I have an issue - it seems like requests library has a very hard time downloading binary files that are larger than usual json/html responses. I have a repository of files that I want to be able to download via HTTP request. It takes very long time when I download a 2 mb file programatically (when I use a browser it is almost immediate).

I use Flask with Connexion framework, thats my code handling file sending:

def get_latest_file(document_id: str):
    doc_bytes = get_bytes(document_id)
    """some logic to get file bytes"""
    return send_file(
        BytesIO(doc_bytes),
        attachment_filename=file_name,
        as_attachment=True,
    )

and connexion yaml:

/documents/{document_id}/download:
    get:
      operationId: src.api.methods.document_details.get_latest_file
      parameters:
        - in: path
          name: document_id
          required: true
          schema:
            type: string
      tags:
        - documents
      responses:
        "200":
          description: OK
          content:
            "application/*":
              schema:
                format: byte
        "404":
          description: NOT FOUND
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        "401":
          description: UNAUTHORIZED
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        "500":
          description: INTERNAL SERVER ERROR

And if I call it via requests it takes forever. The use case is: Get a file from repository service and then forward it down with another http response, so the pipeline looks like this:

user -> middle service -> repository service

and response is similar:

repository service -> middle service -> user

Any thoughts? I use Python 3.7, the server is working on ubuntu 18.04.

Feel free to ask for more info.

Thanks.

sznowak13
  • 11
  • 5
  • Does this answer your question? [Download large file in python with requests](https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests) – tzaman Aug 31 '20 at 12:59
  • @tzaman thanks, but this answers asks about saving a file onto disk space directly, my use case is to forward this file further with another http response. I will update my question to add this information. – sznowak13 Aug 31 '20 at 14:22

0 Answers0