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'
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.
>>> import requests
>>> requests.utils.quote('[email protected]')
'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
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)
Videos
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:
data = {'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:
$ nc -kl 8765
In another:
$ 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):
spam=20&eggs=3
… while the second is this (and has no Content-Type):
{"spam": 20, "eggs": 3}
Short answer with example:
import 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
Python 3
In Python 3, the urllib package has been broken into smaller components. You'll use urllib.parse.quote_plus (note the parse child module)
Copyimport urllib.parse
safe_string = urllib.parse.quote_plus(...)
Python 2
What you're looking for is urllib.quote_plus:
Copysafe_string = urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$')
#Value: 'string_of_characters_like_these%3A%24%23%40%3D%3F%25%5EQ%5E%24'
Python 3
Use urllib.parse.urlencode:
Copy>>> import urllib.parse
>>> f = { 'eventName' : 'myEvent', 'eventDescription' : 'cool event'}
>>> urllib.parse.urlencode(f)
eventName=myEvent&eventDescription=cool+event
Note that this does not do url encoding in the commonly used sense (look at the output). For that use urllib.parse.quote_plus.
Python 2
You need to pass your parameters into urllib.urlencode() as either a mapping (dict), or a sequence of 2-tuples, like:
Copy>>> import urllib
>>> f = { 'eventName' : 'myEvent', 'eventDescription' : 'cool event'}
>>> urllib.urlencode(f)
'eventName=myEvent&eventDescription=cool+event'
When creating the object for the data keyword, simply assign a variable the result of json.dumps(data).
Also, because HTTP POST can accept both url parameters as well as data in the body of the request, and because the requests.post function has a keyword argument named "params", it might be better to use a different variable name for readability. The requests docs use the variable name "payload", so thats what I use.
data = {"name":"Partner13", "email":"[email protected]"}
payload = json.dumps(data)
r = requests.post(uri, data=payload, headers=headers)
Requests automatically URL encodes dictionaries passed as data here. John_GG's solution works because rather than posting a dictionary containing the JSON encoded string in the 'data' field it simply passes the JSON encoded string directly: strings are not automatically encoded. I can't say I understand the reason for this behaviour in Requests but regardless, it is what it is. There is no way to toggle this behaviour off that I can find.
Best of luck with it, Kevin.