4

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)

Elie
  • 337
  • 3
  • 6
  • 14
  • the `%` is used to encode special characters in urls. you can encode the `%` character with this sequence `%25`. see here for more detail: http://www.w3schools.com/tags/ref_urlencode.asp – Tryph Oct 21 '16 at 07:50
  • Thanks a lot! It is working perfectly fine now. – Elie Oct 21 '16 at 07:54

4 Answers4

4

You should have a look at urllib.quote - that should do the trick. Have a look at the docs for reference.

To expand on this answer: The problem is, that % (+ a hexadecimal number) is the escape sequence for special characters in URLs. If you want the server to interpret your % literaly, you need to escape it as well, which is done by replacing it with %25. The aforementioned qoute function does stuff like that for you.

Maurice
  • 11,482
  • 2
  • 25
  • 45
4

Let requests construct the query string for you by passing the parameters in the params argument to requests.get() (see documentation):

api_url = 'http://myApiURL'
params = {'ID': 'something', 'parameter': '%w42'}
r = requests.get(api_url, params=params, auth=(user, pass))

requests should then percent encode the parameters in the query string for you. Having said that, at least with requests version 2.11.1 on my machine, I find that the % is encoded when passing it in the url, so perhaps you could check which version you are using.

Also for basic authentication you can simply pass the user name and password in a tuple as shown above.

mhawke
  • 84,695
  • 9
  • 117
  • 138
3

in requests you should use requests.compat.quote_plus here's take alook

example :

>>> requests.compat.quote_plus('example: parameter=%w42')
'example%3A+parameter%3D%25w42'
Hisham Karam
  • 1,288
  • 17
  • 28
0

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

Elie
  • 337
  • 3
  • 6
  • 14
  • 1
    my comment does not dispense you from using a function which will encode the special characters automatically, as mentionned by Maurice and Hisham Karam – Tryph Oct 21 '16 at 07:57