Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python3 requests use quote instead of quote_plus

I use Python 3 and the requests module/library for querying a REST service.

It seems that requests by default uses urllib.parse.quote_plus() for urlencoding, i.e. spaces are converted to +.

However the REST service I query misinterpretes this as and. So I need to encode spaces as %20, i.e. use urllib.parse.quote() instead.

Is there an easy way to do this with requests? I couldn't find any option in the documentation.

like image 303
absurd Avatar asked Feb 20 '17 15:02

absurd


People also ask

Why can’t we use single quotes in Python?

The reason is that a single quote or double quote itself is a special character we use in our Python program. In many cases, it has been seen that you want to print a string or you want to work with a string.

What is the use of triple quotes in Python?

Actually, we can use triple quotes (i.e., triplet of single quotes or triplet double quotes) to represent the strings containing both single and double quotes to eliminate the need of escaping any. >>> print ('''She said, "Thank you!

How to print ‘withquotes’ in Python?

If you want to print ‘WithQuotes’ in python, this can’t be done with only single (or double) quotes alone, it requires simultaneous use of both. # this code prints the output within quotes. The choice between both the types (single quotes and double quotes) depends on the programmer’s choice.

How to escape a quote in a string in Python?

Put the escaping character before the single quote in the string. You can use the first solution like this: In Python, the backslash is an escaping character. So you can use it like the below: Now suppose you have to print the below line: She said, “You are looking nice”. And I smiled print ("She said, "You are looking nice". And I smiled")


1 Answers

It turns out you can!

from requests.utils import requote_uri
url = "https://www.somerandom.com/?name=Something Cool"
requote_uri(url)

'https://www.somerandom.com/?name=Something%20Cool'

documentation here The requote_uri method is about halfway down the page.

like image 67
gallen Avatar answered Sep 29 '22 20:09

gallen