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
{{ message }}
Repository navigation
User-provided Content-Length header is missing from streaming requests #1168
While also including the content-length that you've set explicitly.
Presumably the h11 library is then discounting the Content-Length. (Since it's invalid to set both Content-Length and Transfer-Encoding.)
I think what we probably want is an explicit ignore case for setting Transfer-Encoding from the stream, if the Content-Length has been set explicitly. Probably something like changing this piece of code:
Along with a test case to verify that a .build_request() operation like this... c.build_request("POST", "https://www.example.org", headers={'Content-Length': '5'}, data=(i for i in b'abcde')) results in the correct headers. (Ie. just the Content-Length one.)
@j178 Okay. It's possible that an intermediary could be stripping it before it gets to the server.
Either way around the issue is valid, since we shouldn't be setting both headers.
To clarify, I noticed this while trying to make requests to an API that checks the presence of the Content-Length header (B2 API, to be precise), so it's definitely not limited to httpbin. For example, if data is a bytes object, httpbin registers the header correctly.
Activity
lovelydinosaur commentedon Aug 11, 2020
Thanks for the report.
You can see what's happening a bit more clearly with this...
Our
.build_requestis including both theTransfer-Encoding: chunkedthat it expects from streaming data...httpx/httpx/_content_streams.py
Line 86 in db4e417
While also including the
content-lengththat you've set explicitly.Presumably the
h11library is then discounting theContent-Length. (Since it's invalid to set bothContent-LengthandTransfer-Encoding.)I think what we probably want is an explicit ignore case for setting
Transfer-Encodingfrom the stream, if theContent-Lengthhas been set explicitly. Probably something like changing this piece of code:httpx/httpx/_models.py
Line 615 in db4e417
From...
To this...
Along with a test case to verify that a
.build_request()operation like this...c.build_request("POST", "https://www.example.org", headers={'Content-Length': '5'}, data=(i for i in b'abcde'))results in the correct headers. (Ie. just the Content-Length one.)lovelydinosaur commentedon Aug 11, 2020
Labelling this as a good first issue, since there's hopefully enough into in the comment for someone to be able to resolve this.
j178 commentedon Aug 11, 2020
Run your scripts and running Wireshark,

httpxactually contains thecontent-lengthheader:lovelydinosaur commentedon Aug 11, 2020
@j178 Okay. It's possible that an intermediary could be stripping it before it gets to the server.
Either way around the issue is valid, since we shouldn't be setting both headers.
j178 commentedon Aug 11, 2020
Yes, I'll open a PR for this.
vaultah commentedon Aug 11, 2020
To clarify, I noticed this while trying to make requests to an API that checks the presence of the Content-Length header (B2 API, to be precise), so it's definitely not limited to httpbin. For example, if
datais abytesobject, httpbin registers the header correctly.