I'm sending a get request with the following data in order to check the banking status of a payment QR Code in Brazil
url = 'https://<bankAPI>/qrcode?qrcode='
data = '00020101021126360014br.gov.bcb.pix0114+55489962650955204000053039865802BR5925TEREZINHA APARECIDA DEL S6008BRASILIA62070503***63040833'
if I put this data into a get request like this response = httpx.get(url+data) and I print the response.url I get
https://<bankAPI>/qrcode?qrcode='00020101021126360014br.gov.bcb.pix0114+55489962650955204000053039865802BR5925TEREZINHA%20APARECIDA%20DEL%20S6008BRASILIA62070503***63040833
as you can see the plus sign + is not being encoded and I'm forced to use the parsing library before passing the data to httpx I thought the encoding was handled directly by httpx, is there anything I'm doing wrong?
I tried requests library and it's the same...so now I'm using the Quote function to parse the data but I think HTTX should handle it directly.
Short answer
Use the
params=argument forhttpx.get()for URL encoding more closely aligned with what you seem to expect it to do:Longer explanation
URL parsing and encoding in
HTTPXis handled inhttpx/_urlparse.py.The characters that get percent-encoded vary with which part of the URL is being processed. For the query string (the part that you're interested in), this is specifically handled in
_urlparse.py:265-267. Thesafeparameter passed toquote()is a string of characters which should not be percent encoded for the given URL component.In this instance,
safeis ultimately set to a string containing the following characters:Conspicuously this includes the
+token, meaning HTTPX will not encode this by default if you build the URL yourself.On the flipside, passing your query parameters properly using the
paramsparameter on thehttpx.get()method does indeed handle this for you via calls tourllib.parse.parse_qs:Repl.it demo
In short - if you're going to pass URL parameters and necessarily need them to be handled as you seem to expect, use the
params=argument.