# HG changeset patch # Parent 469ff344f8fd13e98d8c104dff42d0c974b51576 diff -r 469ff344f8fd Doc/faq/library.rst --- a/Doc/faq/library.rst Sat Jan 31 12:20:40 2015 -0800 +++ b/Doc/faq/library.rst Sun Feb 01 12:34:40 2015 +0000 @@ -682,7 +682,7 @@ import urllib.request ### build the query string - qs = "First=Josephine&MI=Q&Last=Public" + qs = b"First=Josephine&MI=Q&Last=Public" ### connect and send the server a path req = urllib.request.urlopen('http://www.some-server.out-there' diff -r 469ff344f8fd Doc/library/urllib.request.rst --- a/Doc/library/urllib.request.rst Sat Jan 31 12:20:40 2015 -0800 +++ b/Doc/library/urllib.request.rst Sun Feb 01 12:34:40 2015 +0000 @@ -21,15 +21,19 @@ Open the URL *url*, which can be either a string or a :class:`Request` object. - *data* must be a bytes object specifying additional data to be sent to the - server, or ``None`` if no such data is needed. *data* may also be an - iterable object and in that case Content-Length value must be specified in + The *data* argument must be a bytes object specifying additional data + to be sent to the server, or ``None`` if no such data is needed. + The *data* argument may also be an iterable object, + and in that case Content-Length value must be specified in the headers. 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 + The data value should be in the standard + :mimetype:`application/x-www-form-urlencoded` format, + and a ``Content-Type`` header will be sent to indicate this. + (Other formats can be sent using the *data* and + *headers* parameters of a :class:`Request` object.) The :func:`urllib.parse.urlencode` function takes a mapping or sequence of 2-tuples and returns a string in this format. It should be encoded to bytes before being used as the *data* parameter. The charset parameter in @@ -170,11 +174,14 @@ *url* should be a string containing a valid URL. - *data* must be a bytes object specifying additional data to send to the + The *data* argument must 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 only ones that use *data*. The HTTP request will be a POST instead of + a GET when the *data* parameter is provided, unless overridden by + the *method* parameter. If no ``Content-Type`` header field is provided, + the type is set to :mimetype:`application/x-www-form-urlencoded`, + and *data* should be a buffer in that format. The :func:`urllib.parse.urlencode` function takes a mapping or sequence of 2-tuples and returns a string in this format. It should be encoded to bytes @@ -1102,6 +1109,7 @@ >>> import urllib.request >>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi', + ... headers={'Content-type': 'text/plain'}, ... data=b'This data is passed to stdin of the CGI') >>> f = urllib.request.urlopen(req) >>> print(f.read().decode('utf-8')) @@ -1112,13 +1120,14 @@ #!/usr/bin/env python import sys data = sys.stdin.read() - print('Content-type: text-plain\n\nGot Data: "%s"' % data) + print('Content-type: text/plain\n\nGot Data: "%s"' % data) Here is an example of doing a ``PUT`` request using :class:`Request`:: import urllib.request DATA=b'some data' - req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT') + req = urllib.request.Request(url='http://localhost:8080', data=DATA, + headers={'Content-type': 'text/plain'}, method='PUT') f = urllib.request.urlopen(req) print(f.status) print(f.reason)