You can use requests.utils.quote (which is just a link to urllib.parse.quote) to convert your text to url encoded format.
Copy>>> import requests
>>> requests.utils.quote('test+user@gmail.com')
'test%2Buser%40gmail.com'
Answer from MohitC on Stack OverflowYou can use requests.utils.quote (which is just a link to urllib.parse.quote) to convert your text to url encoded format.
Copy>>> import requests
>>> requests.utils.quote('test+user@gmail.com')
'test%2Buser%40gmail.com'
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
Copyimport 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)
Urlencoded sending a JSON string
How to avoid URL encoding done by requests library while generating API key through XML version of Panorama API. Is there an alternate way of generating API without using the requests library?
Suggestion: Improve `urlencode()` handling of boolean values - Ideas - Discussions on Python.org
How to url encode User Search Query Syntax for Management API in Python
Videos
I am trying to generate an API key using Panorama XML API, my password contains special characters like %&#, etc, the requests library URL encodes these special characters and XML doesn't understand URL encoding and I am getting a connection error, what's the best way to fix this. I tried not doing % encoding it doesn't work because requests does it by default.
The reason you're getting JSON is because you're explicitly calling json.dumps to generate a JSON string. Just don't do that, and you won't get a JSON string. In other words, change your first line to this:
Copydata = {'param1': 'value1', 'param2': 'value2'}
As the docs explain, if you pass a dict as the data value, it will be form-encoded, while if you pass a string, it will be sent as-is.
For example, in one terminal window:
Copy$ nc -kl 8765
In another:
Copy$ python3
>>> import requests
>>> d = {'spam': 20, 'eggs': 3}
>>> requests.post("http://localhost:8765", data=d)
^C
>>> import json
>>> j = json.dumps(d)
>>> requests.post("http://localhost:8765", data=j)
^C
In the first terminal, you'll see that the first request body is this (and Content-Type application/x-www-form-urlencoded):
Copyspam=20&eggs=3
… while the second is this (and has no Content-Type):
Copy{"spam": 20, "eggs": 3}
Short answer with example:
Copyimport requests
the_data = {"aaa": 1, "bbb": 2, "ccc": "yeah"}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
# Execute the post
requests.post("http://bla.bla.example.com", data=the_data, headers=headers)
# You have POSTed this HTTP body: aaa=1&bbb=2&ccc=yeah (note, although the content-type is called urlencoded the data is not in the URL but in the http body)
# to this url: "http://bla.bla.example.com"
Requests library does all the JSON to urlencoded string conversion for you
References:
MDN Web docs, Requests lib post url-encoded form