Request builds incorrectly if data values are passed as bytes #3153
Unanswered
harrylloyd-bl
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
OS platform: Windows
Python version: 3.12.0
Installed dependencies and versions (python -m pip freeze):
Conda environment: conda_env.log
httpx - 0.27.0
Code snippet
Error traceback
This fails because the oauth_signature and oauth_body_hash have the
b''
byte object markers included when the body of the url is encodedreq.content = b"oauth_body_hash=b%27myoauthbodyhash%27&oauth_signature=b%27oauthsignature%27"
req.content = b"oauth_body_hash=myoauthbodyhash&oauth_signature=myoauthsignature"
Does the issue exist on HTTP/1.1, or HTTP/2, or both? HTTP/1.1, unsure about /2
Does the issue exist with Client, AsyncClient, or both? Both
When using AsyncClient does the issue exist when using asyncio or trio, or both? Both
This is loosely related to #3115
This will apply to any parameter in
data
, I came across it because the oauth lib I'm using returns the signature and body hash as bytes. If it's best practice that data should be passed as str not bytes then I'm happy to have this closed, but the RequestData typing is currentlyMapping[str, Any]
, so that might be updated to exclude bytes.The issue arises because
Request
calls_content.encode_urlencoded_data
which uses_utils.primitive_value_to_str
to stringify the values indata
._utils.primitive_value_to_str
takes typePrimitiveData = Optional[Union[str, int, float, bool]]
, so isn't expecting bytes. Aside from decoding any bytes values indata
before they're passed tobuild_request
the fix that I've used is to add anelif
to_utils.primitive_value_to_str
to return the utf-8 decoded string iftype == bytes
. The typing for_utils.primitive_value_to_str
would need to change though.Beta Was this translation helpful? Give feedback.
All reactions