[Python-checkins] cpython (merge 3.2 -> default): cpython:Fix the wrong urllib exampls which use str for POST data. Closes

senthil.kumaran python-checkins at python.org
Thu Mar 15 02:11:55 CET 2012


http://hg.python.org/cpython/rev/0345dc184e9a
changeset:   75677:0345dc184e9a
parent:      75675:d97ae725814c
parent:      75676:fddbe9a59d26
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Wed Mar 14 18:11:46 2012 -0700
summary:
  cpython:Fix the wrong urllib exampls which use str for POST data. Closes Issue11261

files:
  Doc/howto/urllib2.rst          |   4 +++-
  Doc/library/urllib.request.rst |  17 ++++++++---------
  2 files changed, 11 insertions(+), 10 deletions(-)


diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst
--- a/Doc/howto/urllib2.rst
+++ b/Doc/howto/urllib2.rst
@@ -115,6 +115,7 @@
               'language' : 'Python' }
 
     data = urllib.parse.urlencode(values)
+    data = data.encode('utf-8') # data should be bytes
     req = urllib.request.Request(url, data)
     response = urllib.request.urlopen(req)
     the_page = response.read()
@@ -179,7 +180,8 @@
               'language' : 'Python' }
     headers = { 'User-Agent' : user_agent }
 
-    data = urllib.parse.urlencode(values)
+    data  = urllib.parse.urlencode(values)
+    data = data.encode('utf-8')
     req = urllib.request.Request(url, data, headers)
     response = urllib.request.urlopen(req)
     the_page = response.read()
diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst
--- a/Doc/library/urllib.request.rst
+++ b/Doc/library/urllib.request.rst
@@ -138,14 +138,13 @@
 
    *url* should be a string containing a valid URL.
 
-   *data* may be a string specifying additional data to send to the
-   server, or ``None`` if no such data is needed.  Currently HTTP
-   requests are the only ones that use *data*, in order to choose between
-   ``'GET'`` and ``'POST'`` when *method* is not specified.
-   *data* should be a buffer in the standard
-   :mimetype:`application/x-www-form-urlencoded` format.  The
-   :func:`urllib.parse.urlencode` function takes a mapping or sequence
-   of 2-tuples and returns a string in this format.
+   *data* may be a bytes object specifying additional data to send to the
+   server, or ``None`` if no such data is needed.  Currently HTTP requests are
+   the only ones that use *data*; the HTTP request will be a POST instead of a
+   GET when the *data* parameter is provided.  *data* should be a buffer in the
+   standard :mimetype:`application/x-www-form-urlencoded` format.  The
+   :func:`urllib.parse.urlencode` function takes a mapping or sequence of
+   2-tuples and returns a string in this format.
 
    *headers* should be a dictionary, and will be treated as if
    :meth:`add_header` was called with each key and value as arguments.
@@ -1183,7 +1182,7 @@
 
    If the *url* uses the :file:`http:` scheme identifier, the optional *data*
    argument may be given to specify a ``POST`` request (normally the request
-   type is ``GET``).  The *data* argument must in standard
+   type is ``GET``).  The *data* argument must be a bytes object in standard
    :mimetype:`application/x-www-form-urlencoded` format; see the
    :func:`urlencode` function below.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list