You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Content-Type when sending data with urlopen() #67549
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-12-06.00:08:26.531>created_at=<Date2015-02-01.12:43:32.468>labels= ['docs']
title='Content-Type when sending data with urlopen()'updated_at=<Date2021-12-06.00:08:26.531>user='https://github.com/vadmium'
Currently the documentation gives the impression that the “data” parameter to Request() has to be in the application/x-www-form-urlencoded format. However I suspect that you can override the type by supplying a Content-Type header, and I would like to document this; see uploaded patch.
I noticed that test_urllib2.HandlerTests.test_http() already seems to test the default Content-Type and a custom Content-Type with a Request() object, although I did not see a test for the default Content-Type when supplying “data” directly to urlopen().
Also I understand the “charset” parameter on application/x-www-form-urlencoded is not standardized. Would it correspond to the encoding of the %XX codes from urlencode(), which is typically UTF-8, not Latin-1? Or would it correspond to the subsequent string-to-bytes encoding stage, which could just be ASCII since non-ASCII characters are already encoded? Maybe it would be best to drop the advice to set a “charset” parameter. It was added for bpo-11082.
Updated patch to explain that a Request object is generated internally for urlopen(data=...), and added a test to confirm. Also removed some confusing dead code.
The documentation looks contradictory. "The *data* argument must be", but "The *data* argument may also be". "must be a bytes object", but "If *data* is a buffer".
Why not write just "The data argument must be a bytes-like object, an iterable of bytes-like objects, or None"? It doesn't depend if url is a string or a Request object.
AFAIK the data argument of Request can be an iterable of bytes-like objects in additional to a bytes-like object or None.
The note about the application/x-www-form-urlencoded format is applied not only to a bytes object, but to an iterable of bytes-like objects too. I.e. to any acceptable value except None.
I think we should avoid mentioning bytes-like objects until bpo-23740 (http.client support), bpo-23756 (clarify definition), and/or SSLSocket.sendall() support are sorted out.
Changes in non-urlencoded.3.patch:
Removed iterable object as direct urlopen() argument, since that would require a custom Content-Length and therefore a custom Request object
Removed Content-Type discussion from urlopen() for similar reasons
Added iterable object to Request constructor (already tested)
Clarified default Content-Type whenever data is not None
Added a test for default Content-Type with iterable object
Activity
vadmium commentedon Feb 1, 2015
Currently the documentation gives the impression that the “data” parameter to Request() has to be in the application/x-www-form-urlencoded format. However I suspect that you can override the type by supplying a Content-Type header, and I would like to document this; see uploaded patch.
I noticed that test_urllib2.HandlerTests.test_http() already seems to test the default Content-Type and a custom Content-Type with a Request() object, although I did not see a test for the default Content-Type when supplying “data” directly to urlopen().
Also I understand the “charset” parameter on application/x-www-form-urlencoded is not standardized. Would it correspond to the encoding of the %XX codes from urlencode(), which is typically UTF-8, not Latin-1? Or would it correspond to the subsequent string-to-bytes encoding stage, which could just be ASCII since non-ASCII characters are already encoded? Maybe it would be best to drop the advice to set a “charset” parameter. It was added for bpo-11082.
vadmium commentedon Feb 2, 2015
Updated patch to explain that a Request object is generated internally for urlopen(data=...), and added a test to confirm. Also removed some confusing dead code.
serhiy-storchaka commentedon Mar 21, 2015
The documentation looks contradictory. "The *data* argument must be", but "The *data* argument may also be". "must be a bytes object", but "If *data* is a buffer".
Why not write just "The data argument must be a bytes-like object, an iterable of bytes-like objects, or None"? It doesn't depend if url is a string or a Request object.
AFAIK the data argument of Request can be an iterable of bytes-like objects in additional to a bytes-like object or None.
The note about the application/x-www-form-urlencoded format is applied not only to a bytes object, but to an iterable of bytes-like objects too. I.e. to any acceptable value except None.
vadmium commentedon Mar 31, 2015
I think we should avoid mentioning bytes-like objects until bpo-23740 (http.client support), bpo-23756 (clarify definition), and/or SSLSocket.sendall() support are sorted out.
Changes in non-urlencoded.3.patch:
vadmium commentedon Nov 7, 2015
Patch 4 is just updated to avoid conflicts with the current code. Changes are the same.
vadmium commentedon Nov 7, 2015
Spotted a docstring that needed updating
vadmium commentedon Jun 17, 2016
Fixed conflicts with recent changes
iritkatriel commentedon Nov 27, 2021
Martin, I think you fixed this in
3c0d0ba#diff-533bd604631e0e26ce55dfa75a878788f3c4d7d7ccb3bbaeaa2ee2a9c956ffe8