pythonurlpython-requestspercent-encoding

Python requests: URL with percent character


I have been searching all over the place for this, but I couldn't solve my issue.

I am using a local API to fetch some data, in that API, the wildcard is the percent character %.

The URL is like so : urlReq = 'http://myApiURL?ID=something&parameter=%w42'

And then I'm passing this to the get function:

req = requests.get(urlReq,auth=HTTPBasicAuth(user, pass))

And get the following error: InvalidURL: Invalid percent-escape sequence: 'w4'

I have tried escaping the % character using %%, but in vain. I also tried the following:

urlReq = 'http://myApiURL?ID=something&parameter=%sw42' % '%' but didn't work as well.

Does anyone know how to solve this?

PS I'm using Python 2.7.8 :: Anaconda 1.9.1 (64-bit)


Solution

  • Credits to @Tryph:

    the % is used to encode special characters in urls. you can encode the % character with this sequence %25. see here for more detail: w3schools.com/tags/ref_urlencode.asp