Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7596aea

Browse files
committed
cpython:Fix the wrong urllib exampls which use str for POST data. Closes Issue11261
2 parents 3d7c878 + 87684e6 commit 7596aea

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

Doc/howto/urllib2.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ library. ::
115115
'language' : 'Python' }
116116

117117
data = urllib.parse.urlencode(values)
118+
data = data.encode('utf-8') # data should be bytes
118119
req = urllib.request.Request(url, data)
119120
response = urllib.request.urlopen(req)
120121
the_page = response.read()
@@ -179,7 +180,8 @@ Explorer [#]_. ::
179180
'language' : 'Python' }
180181
headers = { 'User-Agent' : user_agent }
181182

182-
data = urllib.parse.urlencode(values)
183+
data = urllib.parse.urlencode(values)
184+
data = data.encode('utf-8')
183185
req = urllib.request.Request(url, data, headers)
184186
response = urllib.request.urlopen(req)
185187
the_page = response.read()

Doc/library/urllib.request.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,13 @@ The following classes are provided:
138138

139139
*url* should be a string containing a valid URL.
140140

141-
*data* may be a string specifying additional data to send to the
142-
server, or ``None`` if no such data is needed. Currently HTTP
143-
requests are the only ones that use *data*, in order to choose between
144-
``'GET'`` and ``'POST'`` when *method* is not specified.
145-
*data* should be a buffer in the standard
146-
:mimetype:`application/x-www-form-urlencoded` format. The
147-
:func:`urllib.parse.urlencode` function takes a mapping or sequence
148-
of 2-tuples and returns a string in this format.
141+
*data* may be a bytes object specifying additional data to send to the
142+
server, or ``None`` if no such data is needed. Currently HTTP requests are
143+
the only ones that use *data*; the HTTP request will be a POST instead of a
144+
GET when the *data* parameter is provided. *data* should be a buffer in the
145+
standard :mimetype:`application/x-www-form-urlencoded` format. The
146+
:func:`urllib.parse.urlencode` function takes a mapping or sequence of
147+
2-tuples and returns a string in this format.
149148

150149
*headers* should be a dictionary, and will be treated as if
151150
:meth:`add_header` was called with each key and value as arguments.
@@ -1183,7 +1182,7 @@ some point in the future.
11831182

11841183
If the *url* uses the :file:`http:` scheme identifier, the optional *data*
11851184
argument may be given to specify a ``POST`` request (normally the request
1186-
type is ``GET``). The *data* argument must in standard
1185+
type is ``GET``). The *data* argument must be a bytes object in standard
11871186
:mimetype:`application/x-www-form-urlencoded` format; see the
11881187
:func:`urlencode` function below.
11891188

0 commit comments

Comments
 (0)