requests / toolbelt

A toolbelt of useful classes and functions to be used with python-requests
https://toolbelt.readthedocs.org
Other
999 stars 186 forks source link

Support HTTPResponse object as file in MultipartEncoder #369

Closed viperadnan-git closed 1 year ago

viperadnan-git commented 1 year ago

Added content length for HTTPResponse in MultipartEncoder to support posting multipart data from streaming response.

Use case:

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

# Define the URLs
download_url = "download_url"
upload_url = 'upload_url'

# Download the content as a stream
with requests.get(download_url, stream=True) as download_response:
    # Check if the download was successful
    if download_response.status_code == 200:

        encoder = MultipartEncoder(
            fields={'file': ('10mb.bin', download_response.raw, 'application/octet-stream')}
        )

        # Stream the downloaded content directly to the upload URL
        with requests.post(upload_url, data=encoder, headers={"Content-Type": encoder.content_type}) as upload_response:
            if upload_response.status_code == 200:
                print("Upload successful!")
            else:
                print(f"Upload failed with status code: {upload_response.status_code}")
    else:
        print(f"Failed to download content. Status code: {download_response.status_code}")

Tested with multiple URLs Example URLs: Download URL: https://mirror.nforce.com/pub/speedtests/10mb.bin Upload URL: https://file.io

sigmavirus24 commented 1 year ago

This works in very few use cases and thus is not something we will support. Anyone that would attempt to use this with a URL they've requested that does not have a Content-Length will result in an invalid request. The content-length will be wrong and way too short as demonstrated by you defaulting the retrieval of the header to 0.

As a result, we will not be merging this.

viperadnan-git commented 1 year ago

Thank you for explanation. I will try to cover all the edge cases.

sigmavirus24 commented 1 year ago

You don't need to. This functionality already exists although it seems whomever added it didn't add it to the documentation: https://github.com/requests/toolbelt/blob/c5ac5f3b5c7ed599703e0deb4fd31ce7998711ed/requests_toolbelt/multipart/encoder.py#L579

viperadnan-git commented 1 year ago

Thank you for your response, this is exactly what I needed.

On Mon, 21 Aug 2023, 12:37 am Ian Stapleton Cordasco, < @.***> wrote:

You don't need to. This functionality already exists although it seems whomever added it didn't add it to the documentation: https://github.com/requests/toolbelt/blob/c5ac5f3b5c7ed599703e0deb4fd31ce7998711ed/requests_toolbelt/multipart/encoder.py#L579

— Reply to this email directly, view it on GitHub https://github.com/requests/toolbelt/pull/369#issuecomment-1685367798, or unsubscribe https://github.com/notifications/unsubscribe-auth/AONR54V2CXKRZSYI2WKSQW3XWJN57ANCNFSM6AAAAAA3XFJ6HQ . You are receiving this because you authored the thread.Message ID: @.***>