diff options
author | Senthil Kumaran <orsenthil@gmail.com> | 2011-02-11 11:25:47 (GMT) |
---|---|---|
committer | Senthil Kumaran <orsenthil@gmail.com> | 2011-02-11 11:25:47 (GMT) |
commit | 2933312fe71f15d127009c872cd1e5f98a993999 (patch) | |
tree | 03f8751cfc59abde72fc3379ebfd14a82d6718d0 /Doc | |
parent | 44028d866336743dc2e3d62daa02d4a8ea318d81 (diff) | |
download | cpython-2933312fe71f15d127009c872cd1e5f98a993999.zip cpython-2933312fe71f15d127009c872cd1e5f98a993999.tar.gz cpython-2933312fe71f15d127009c872cd1e5f98a993999.tar.bz2 |
Fixed issue11082 - Reject str for POST data with a TypeError. Document the need to explicitly encode to bytes when using urlencode.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/urllib.parse.rst | 11 | ||||
-rw-r--r-- | Doc/library/urllib.request.rst | 6 |
2 files changed, 12 insertions, 5 deletions
diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst index 6a143c5..a6d7267 100644 --- a/Doc/library/urllib.parse.rst +++ b/Doc/library/urllib.parse.rst @@ -140,6 +140,7 @@ or on combining URL components into a URL string. Use the :func:`urllib.parse.urlencode` function to convert such dictionaries into query strings. + .. versionchanged:: 3.2 Add *encoding* and *errors* parameters. @@ -506,9 +507,10 @@ task isn't already covered by the URL parsing functions above. .. function:: urlencode(query, doseq=False, safe='', encoding=None, errors=None) Convert a mapping object or a sequence of two-element tuples, which may - either be a :class:`str` or a :class:`bytes`, to a "percent-encoded" string, - suitable to pass to :func:`urlopen` above as the optional *data* argument. - This is useful to pass a dictionary of form fields to a ``POST`` request. + either be a :class:`str` or a :class:`bytes`, to a "percent-encoded" + 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* @@ -525,6 +527,9 @@ task isn't already covered by the URL parsing functions above. 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. + Refer to :ref:`urllib examples <urllib-examples>` to find out how urlencode + method can be used for generating query string for a URL or data for POST. + .. versionchanged:: 3.2 Query parameter supports bytes and string objects. diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index 724310b..2b98c83 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -967,7 +967,7 @@ when the Python installation supports SSL. :: >>> import urllib.request >>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi', - ... data='This data is passed to stdin of the CGI') + ... data=b'This data is passed to stdin of the CGI') >>> f = urllib.request.urlopen(req) >>> print(f.read().decode('utf-8')) Got Data: "This data is passed to stdin of the CGI" @@ -1043,11 +1043,13 @@ containing parameters:: >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params) >>> print(f.read().decode('utf-8')) -The following example uses the ``POST`` method instead:: +The following example uses the ``POST`` method instead. Note that params output +from urlencode is encoded to bytes before it is sent to urlopen as data:: >>> import urllib.request >>> import urllib.parse >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) + >>> params = params.encode('utf-8') >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query", params) >>> print(f.read().decode('utf-8')) |