Monu Yadav
Monu Yadav

Reputation: 570

How to encode url path?

I have to make network requests to an API endpoint. However requests only encodes query parameters and not the url path. Consider below for exmaple https://example.com/Nova#123

The server endpoint however accepts https://example.com/Nova%23123. How can I do this with requests? String replacement is not a solution considering other special characters.

Upvotes: 0

Views: 388

Answers (1)

Maurice Meyer
Maurice Meyer

Reputation: 18136

There is urllib.parse.quote:

from urllib.parse import urljoin, quote
print(urljoin('https://example.com', quote('Nova#123')))

Out:

https://example.com/Nova%23123

Upvotes: 1

Related Questions