You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Context: uploading files to AWS S3 via requests. Files may be empty. When attempting to upload an empty file, a response of 501: Not implemented is received. This seems to be because, when content length is 0, a Transfer-Encoding: chunked header is automatically added. S3 does not support the encoding.
Expected Result
When an empty file PUT is attempted via requests.put, a Content-Length: 0 header should be set on the request. A Transfer-Encoding: chunked header should not be set automatically.
Actual Result
When an empty file PUT is attempted via requests.put, a Transfer-Encoding: chunked header is set. No Content-Length header is set. If a Content-Length: 0 header is set explicitly, the Transfer-Encoding: chunked header is still set.
Reproduction Steps
If AWS account, bucket, credentials for principal with PutObject permissions aren't available:
importrequestsfile=open(<path_to_empty_file>, "rb")
session=requests.session()
link="http://httpbin.org/put"# using requests.put yields the same results. Using session.put here to examine request# with S3 instead of httpbin.org, the call below results in a 501. Note the request headersres_no_CL=session.put(link, data=file, headers={"Content-Type": "text/plain"})
print(res_no_CL.request.headers)
# with S3 instead of httpbin.org, the call below results in a 501. With httpbin.org it returns# ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))# so can't examine headers, but I assume behaviour is consistentres_explicit_CL=session.put(link, data=file, headers={"Content-Type": "text/plain", "Content-Length": "0"})
print(res_explicit_CL.request.headers)
If AWS prerequisites above are available:
importrequestsimportboto3s3=boto3.client('s3')
file=open(<path_to_empty_file>, "rb")
session=requests.session()
link=s3.generate_presigned_url(ClientMethod="put_object", Params={"Bucket": "<your_bucket>", "Key": "<path_in_bucket>", "ContentType": "text/plain"})
# using requests.put yields the same results. Using session.put here to examine request# the call below results in a 501. Note the request headersres_no_CL=session.put(link, data=file, headers={"Content-Type": "text/plain"})
print(res_no_CL.request.headers)
# the call below results in a 501. Note the request headersres_explicit_CL=session.put(link, data=file, headers={"Content-Type": "text/plain", "Content-Length": "0"})
print(res_explicit_CL.request.headers)
Context: uploading files to AWS S3 via requests. Files may be empty. When attempting to upload an empty file, a response of
501: Not implemented
is received. This seems to be because, when content length is 0, aTransfer-Encoding: chunked
header is automatically added. S3 does not support the encoding.Expected Result
When an empty file PUT is attempted via
requests.put
, aContent-Length: 0
header should be set on the request. ATransfer-Encoding: chunked
header should not be set automatically.Actual Result
When an empty file PUT is attempted via
requests.put
, aTransfer-Encoding: chunked
header is set. NoContent-Length
header is set. If aContent-Length: 0
header is set explicitly, theTransfer-Encoding: chunked
header is still set.Reproduction Steps
If AWS account, bucket, credentials for principal with PutObject permissions aren't available:
If AWS prerequisites above are available:
System Information
The text was updated successfully, but these errors were encountered: