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

Skip to content

Commit 87684e6

Browse files
committed
Fix the wrong urllib exampls which use str for POST data. Closes Issue11261
1 parent 4552b2e commit 87684e6

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

Doc/howto/urllib2.rst

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

110110
data = urllib.parse.urlencode(values)
111+
data = data.encode('utf-8') # data should be bytes
111112
req = urllib.request.Request(url, data)
112113
response = urllib.request.urlopen(req)
113114
the_page = response.read()
@@ -172,7 +173,8 @@ Explorer [#]_. ::
172173
'language' : 'Python' }
173174
headers = { 'User-Agent' : user_agent }
174175

175-
data = urllib.parse.urlencode(values)
176+
data = urllib.parse.urlencode(values)
177+
data = data.encode('utf-8')
176178
req = urllib.request.Request(url, data, headers)
177179
response = urllib.request.urlopen(req)
178180
the_page = response.read()

Doc/library/urllib.request.rst

Lines changed: 11 additions & 12 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*; the HTTP request will
144-
be a POST instead of a GET when the *data* parameter is provided.
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.
@@ -1122,10 +1121,10 @@ some point in the future.
11221121
size in response to a retrieval request.
11231122

11241123
If the *url* uses the :file:`http:` scheme identifier, the optional *data*
1125-
argument may be given to specify a ``POST`` request (normally the request type
1126-
is ``GET``). The *data* argument must in standard
1127-
:mimetype:`application/x-www-form-urlencoded` format; see the :func:`urlencode`
1128-
function below.
1124+
argument may be given to specify a ``POST`` request (normally the request
1125+
type is ``GET``). The *data* argument must be a bytes object in standard
1126+
:mimetype:`application/x-www-form-urlencoded` format; see the
1127+
:func:`urlencode` function below.
11291128

11301129
:func:`urlretrieve` will raise :exc:`ContentTooShortError` when it detects that
11311130
the amount of data available was less than the expected amount (which is the

0 commit comments

Comments
 (0)