diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst --- a/Doc/library/urllib.parse.rst +++ b/Doc/library/urllib.parse.rst @@ -515,6 +515,7 @@ string. The resultant string must be converted to bytes using the user-specified encoding before it is sent to :func:`urlopen` as the optional *data* argument. + The resulting string is a series of ``key=value`` pairs separated by ``'&'`` characters, where both *key* and *value* are quoted using :func:`quote_plus` above. When a sequence of two-element tuples is used as the *query* @@ -531,6 +532,22 @@ To reverse this encoding process, :func:`parse_qs` and :func:`parse_qsl` are provided in this module to parse query strings into Python data structures. + If the resultant string is to be used as *data* argument for + :func:`urlopen`, the string could be encoded using ISO-8859-1 which is the + default encoding for POST data used by urllib or the user can also encode using + a custom encoding , in which case, Content-Type: header should specify the + encoding value in addition to 'application/x-www-form-urlencoded' which is + specified for POST data. + + For Example, utf-8 encoding can be used as follows. + + :: + + >>> sdata = urllib.parse.urlencode({"ሀ":"ሢ"}) + >>> bytesdata = sdata.encode('utf-8') + >>> reqheaders=dict([("Content-Type"," application/x-www-form-urlencoded ; charset=utf-8")]) + >>> urllib.request.Request('http://www.example.com',bytesdata,reqheaders) + Refer to :ref:`urllib examples ` to find out how urlencode method can be used for generating query string for a URL or data for POST.