Tools, FAQ, Tutorials:
Using urllib.parse.urlencode()
How to use urllib.parse.urlencode() function to encode HTTP POST data? My form data has special characters.
✍: FYIcenter.com
If your form data has spaces, equal signs, and other special characters,
you need to call urllib.parse.urlencode() to encode it before passing it
to the urlopen() function.
The urlencode() function takes a list name-value pairs in a JSON format, and returns a urlencoded string of name=value&.... You have to use bytes() function to convert urlencoded string to a binary before calling urlopen() function.
data = urllib.request.urlopen({'name':'value',...})
data = bytes(data,'utf8')
Here is a Python example on how to send a POST request with body data that has special characters:
>>> import urllib
>>> url = 'http://httpbin.org/post'
>>> form = {'user':'fyicenter', 'msg':'hello world!'}
>>> data = urllib.parse.urlencode(form)
>>> data = bytes(data,'utf8')
>>> r = urllib.request.urlopen(url,data)
>>> b = r.read()
>>> print(b)
b'{\n "args": {}, \n "data": "", \n "files": {}, \n
"form": {\n "msg": "hello world!", \n "user": "fyicenter"\n }, \n
"headers": {\n "Accept-Encoding": "identity", ...}, \n
"json": null, \n
"url": "http://httpbin.org/post"\n}\n'
⇒ Using urllib.request.Request Object
⇐ HTTP POST with urllib.request
2018-09-13, ∼6878🔥, 0💬
Popular Posts:
How to access Query String parameters from "context.Request.Url.Que ry"object in Azure API Policy? Q...
What is the Azure AD v1.0 OpenID Metadata Document? Azure AD v1.0 OpenID Metadata Document is an onl...
Where to find EPUB Sample Files? Here is a list of EPUB sample files collected by FYIcenter.com team...
How To Truncate an Array in PHP? If you want to remove a chunk of values from an array, you can use ...
How to add images to my EPUB books Images can be added into book content using the XHTML "img" eleme...