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

Skip to content

Commit f7e8d07

Browse files
committed
Issue #25576: Merge www-form-urlencoded doc from 3.4 into 3.5
2 parents 6e70131 + cda85a0 commit f7e8d07

3 files changed

Lines changed: 16 additions & 30 deletions

File tree

Doc/howto/urllib2.rst

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

117117
data = urllib.parse.urlencode(values)
118-
data = data.encode('utf-8') # data should be bytes
118+
data = data.encode('ascii') # data should be bytes
119119
req = urllib.request.Request(url, data)
120120
with urllib.request.urlopen(req) as response:
121121
the_page = response.read()
@@ -180,8 +180,8 @@ Explorer [#]_. ::
180180
'language' : 'Python' }
181181
headers = { 'User-Agent' : user_agent }
182182

183-
data = urllib.parse.urlencode(values)
184-
data = data.encode('utf-8')
183+
data = urllib.parse.urlencode(values)
184+
data = data.encode('ascii')
185185
req = urllib.request.Request(url, data, headers)
186186
with urllib.request.urlopen(req) as response:
187187
the_page = response.read()

Doc/library/urllib.parse.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,11 @@ task isn't already covered by the URL parsing functions above.
525525
errors=None, quote_via=quote_plus)
526526

527527
Convert a mapping object or a sequence of two-element tuples, which may
528-
contain :class:`str` or :class:`bytes` objects, to a "percent-encoded"
529-
string. If the resultant string is to be used as a *data* for POST
530-
operation with :func:`~urllib.request.urlopen` function, then it should be
531-
properly encoded to bytes, otherwise it would result in a :exc:`TypeError`.
528+
contain :class:`str` or :class:`bytes` objects, to a percent-encoded ASCII
529+
text string. If the resultant string is to be used as a *data* for POST
530+
operation with the :func:`~urllib.request.urlopen` function, then
531+
it should be encoded to bytes, otherwise it would result in a
532+
:exc:`TypeError`.
532533

533534
The resulting string is a series of ``key=value`` pairs separated by ``'&'``
534535
characters, where both *key* and *value* are quoted using the *quote_via*

Doc/library/urllib.request.rst

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@ The :mod:`urllib.request` module defines the following functions:
3636
*data* should be a buffer in the standard
3737
:mimetype:`application/x-www-form-urlencoded` format. The
3838
:func:`urllib.parse.urlencode` function takes a mapping or sequence of
39-
2-tuples and returns a string in this format. It should be encoded to bytes
40-
before being used as the *data* parameter. The charset parameter in
41-
``Content-Type`` header may be used to specify the encoding. If charset
42-
parameter is not sent with the Content-Type header, the server following the
43-
HTTP 1.1 recommendation may assume that the data is encoded in ISO-8859-1
44-
encoding. It is advisable to use charset parameter with encoding used in
45-
``Content-Type`` header with the :class:`Request`.
39+
2-tuples and returns an ASCII text string in this format. It should
40+
be encoded to bytes before being used as the *data* parameter.
4641

4742
urllib.request module uses HTTP/1.1 and includes ``Connection:close`` header
4843
in its HTTP requests.
@@ -180,16 +175,9 @@ The following classes are provided:
180175
the only ones that use *data*; the HTTP request will be a POST instead of a
181176
GET when the *data* parameter is provided. *data* should be a buffer in the
182177
standard :mimetype:`application/x-www-form-urlencoded` format.
183-
184178
The :func:`urllib.parse.urlencode` function takes a mapping or sequence of
185-
2-tuples and returns a string in this format. It should be encoded to bytes
186-
before being used as the *data* parameter. The charset parameter in
187-
``Content-Type`` header may be used to specify the encoding. If charset
188-
parameter is not sent with the Content-Type header, the server following the
189-
HTTP 1.1 recommendation may assume that the data is encoded in ISO-8859-1
190-
encoding. It is advisable to use charset parameter with encoding used in
191-
``Content-Type`` header with the :class:`Request`.
192-
179+
2-tuples and returns an ASCII string in this format. It should be
180+
encoded to bytes before being used as the *data* parameter.
193181

194182
*headers* should be a dictionary, and will be treated as if
195183
:meth:`add_header` was called with each key and value as arguments.
@@ -202,7 +190,7 @@ The following classes are provided:
202190
``"Python-urllib/2.6"`` (on Python 2.6).
203191

204192
An example of using ``Content-Type`` header with *data* argument would be
205-
sending a dictionary like ``{"Content-Type":" application/x-www-form-urlencoded;charset=utf-8"}``.
193+
sending a dictionary like ``{"Content-Type": "application/x-www-form-urlencoded"}``.
206194

207195
The final two arguments are only of interest for correct handling
208196
of third-party HTTP cookies:
@@ -1230,7 +1218,7 @@ every :class:`Request`. To change this::
12301218
opener.open('http://www.example.com/')
12311219

12321220
Also, remember that a few standard headers (:mailheader:`Content-Length`,
1233-
:mailheader:`Content-Type` without charset parameter and :mailheader:`Host`)
1221+
:mailheader:`Content-Type` and :mailheader:`Host`)
12341222
are added when the :class:`Request` is passed to :func:`urlopen` (or
12351223
:meth:`OpenerDirector.open`).
12361224

@@ -1253,11 +1241,8 @@ from urlencode is encoded to bytes before it is sent to urlopen as data::
12531241
>>> import urllib.request
12541242
>>> import urllib.parse
12551243
>>> data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
1256-
>>> data = data.encode('utf-8')
1257-
>>> request = urllib.request.Request("http://requestb.in/xrbl82xr")
1258-
>>> # adding charset parameter to the Content-Type header.
1259-
>>> request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
1260-
>>> with urllib.request.urlopen(request, data) as f:
1244+
>>> data = data.encode('ascii')
1245+
>>> with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f:
12611246
... print(f.read().decode('utf-8'))
12621247
...
12631248

0 commit comments

Comments
 (0)