Reputation: 3015
I'm trying to write a function that does a GET request to download a file from server A, then immediately POST the file to server B.
I'm trying to look for a way to do it by streaming the output of the GET request into the input of the POST request.
The following article discusses doing this in nodeJS. How would I do this with Python Requests?
Upvotes: 3
Views: 4016
Reputation: 1123560
You can pass the raw connection object from a streaming GET request, as a file to a POST request:
r = requests.get(get_url, stream=True)
if r.status_code == 200:
r.raw.decode_content = True # decompress as you read
files = {
'fieldname': ('filename', r.raw, r.headers['Content-Type'])
}
requests.post(post_url, files=files)
To use the response.raw
file-like object will not, by default, decode compressed responses (with GZIP or deflate), but you can force it to decompress for you anyway by setting the decode_content
attribute to True
(requests sets it to False
to control decoding itself).
I assumed that the file was to be POSTed as part of a multipart/form-data
request. If you need to post the data directly, use data=r.raw
instead.
Upvotes: 11