Issue with encode_request and encode_urlencoded_data
#3115
Unanswered
hekhuisk
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I found a couple of issues while using this library.
AsyncClient.post()takes indatawhich is supposed to beRequestDataorNone. Looking at theRequestDatatype it saysMapping[str, Any]. Then insideencode_requestit also takes indatawhich is supposed to beRequestDataorNone. However, the first line of that method is checkingif data is not None and not isinstance(data, Mapping)which implies it can be a type besidesRequestDataorNone. That means a typechecker does not like you passingbytesfordata, even though it is a valid type to pass (yes I know it says it is deprecated, but having something deprecated should not invalidate type hinting).If you do pass a
Mapping, in my case adict, toAsyncClient.post()for thedatafield, and thatdicthappens to contain a key/value pair where the value is anotherdictcontaining a value ofNone, then you end up with a string that says"None"in it. The reason is because of this line inencode_urlencoded_data.The fix that is needed is to check
if isinstance(value, Mapping)and handle properly converting the values in it to a string. You also need to recursively go through all values to check for more nestedMappings. This applies to the check forlistandtupleas well where it doesplain_data.extend([(key, primitive_value_to_str(item)) for item in value]). Ifitemis aMappingyou run into the same issue.I found Form-encoded data with
Noneas the value gets passed as string "None" #1500 and see that the choice was made to always convertNoneto"". I've had many use cases whereNoneis supposed to representnulland not empty string. So if I make a call out to something that can beNoneor astrand it receives""it will becomestrinstead of properly becomingNone. PerhapsRequestshould take a flag callednone_as_nullthat ifTruewould putnullin the serialized JSON instead of"".?Beta Was this translation helpful? Give feedback.
All reactions