user3435964
user3435964

Reputation: 849

URI encoding in Python Requests package

I am using python requests package to get results from a API and the URL contains + sign in it. but when I use requests.get, the request is failing as the API is not able to understand + sign. how ever if I replace + sign with %2B (URI Encoding) the request is successful.

Is there way to encode these characters, so that I encode the URL while passing it to the requests package

Error: test [email protected] does not exist
API : https://example.com/[email protected]

Upvotes: 43

Views: 85072

Answers (2)

Chris
Chris

Reputation: 34075

In Python 3+, one can URL-encode any string using the urllib.parse.quote() (alternatively, you could use requests.utils.quote(), which is essentially using the same quote() function from urllib.parse module). This function is intended for quoting the path section of a URL, not the whole URL. It will essentially replace any special characters in a string using the %xx escape. In Python 2.x, the quote() function can be accessed directly from the urllib package, i.e., urllib.quote().

Note that the quote() function considers / characters safe by default. Hence, in case you are passing another URL to the path section (or the query string) of the URL and would like to encode / characters as well, you could do so by setting the safe parameter to '', i.e., an empty string.

Example

import requests
from urllib.parse import quote 

base_url = 'https://example.com/'
path_param = 'https://raw.githubusercontent.com/ultralytics/yolov5/master/data/images/zidane.jpg'
url = base_url + quote(path_param, safe='')
r = requests.get(url)
print(r.request.url)

Upvotes: 1

MohitC
MohitC

Reputation: 4781

You can use requests.utils.quote (which is just a link to urllib.parse.quote) to convert your text to url encoded format.

>>> import requests
>>> requests.utils.quote('[email protected]')
'test%2Buser%40gmail.com'

Upvotes: 86

Related Questions