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
urllib.request.urlopen sends POST data as query string #84056
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=<Date2020-03-09.06:58:17.848>created_at=<Date2020-03-06.11:45:28.108>labels= ['3.7', 'invalid', 'type-bug', 'library']
title='urllib.request.urlopen sends POST data as query string'updated_at=<Date2020-03-09.06:58:17.847>user='https://bugs.python.org/henrik242'
But why can't the payload be in the Request object?
From the api docs:
class urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)
data must be an 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 supported object types include bytes, file-like objects, and iterables.
method should be a string that indicates the HTTP request method that will be used (e.g. 'HEAD'). If provided, its value is stored in the method attribute and is used by get_method(). The default is 'GET' if data is None or 'POST' otherwise.
henrik242 commented on Mar 6, 2020
henrik242mannequin
MannequinAuthor
More actions
Also, it seems that urllib.urlopen just creates a similar Request object when given a data paramenter:
defopen(self, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
# accept a URL or a Request objectifisinstance(fullurl, str):
req=Request(fullurl, data)
else:
req=fullurlifdataisnotNone:
req.data=data
Activity
henrik242 commentedon Mar 6, 2020
curl correctly posts data to Solr:
The solr query log says:
[20200306T111354,131] [my_coll_shard1_replica_n85] webapp=/solr path=/update params={commit=true} status=0 QTime=96
I'm trying to do the same thing with Python:
But now the solr query log shows that the POST data has been added to the query param string:
[20200306T112358,780] [my_coll_shard1_replica_n87] webapp=/solr path=/update params={commit=true&<add><doc><field+name="key">KEY__9927.1</field><field+name%3D"value">{"result":0,"jobId":"9459695","jobNumber":"9927.1"}</field></doc></add>} status=0 QTime=30
What is happening here?
apetresc commentedon Mar 6, 2020
This is not a bug, you've just misunderstood the urllib API. If you want to pass POST data as a payload, it's the second
dataparameter tourlopen: https://bugs.python.org/?@action=confrego&otk=KX9AqsI0JnOLkplIY1AGKXAmDKa38COyapetresc commentedon Mar 6, 2020
(Oops, that was a bad paste! I meant this link: https://docs.python.org/2/library/urllib.html#urllib.urlopen)
henrik242 commentedon Mar 6, 2020
But why can't the payload be in the Request object?
From the api docs:
https://docs.python.org/3.7/library/urllib.request.html#urllib.request.Request
henrik242 commentedon Mar 6, 2020
Further:
method should be a string that indicates the HTTP request method that will be used (e.g. 'HEAD'). If provided, its value is stored in the method attribute and is used by get_method(). The default is 'GET' if data is None or 'POST' otherwise.
henrik242 commentedon Mar 6, 2020
Also, it seems that urllib.urlopen just creates a similar Request object when given a data paramenter:
From https://github.com/python/cpython/blob/3.7/Lib/urllib/request.py#L507 via https://github.com/python/cpython/blob/3.7/Lib/urllib/request.py#L222
henrik242 commentedon Mar 6, 2020
The following gives the same failing result too :(
I guess I'll have to whip out Wireshark and see what's going on.
henrik242 commentedon Mar 6, 2020
Here's the wireshark output. It seems that urllib adds a "Connection: close" which curl doesn't. Solr doesn't seem to like that.
Curl message:
POST /solr/my_coll/update?commit=true HTTP/1.1
Host: solr.example.no:12699
User-Agent: curl/7.64.1
Accept: */*
Content-Length: 138
Content-Type: application/x-www-form-urlencoded
<add><doc><field name="key">KEY__9927.1</field><field name="value">{"result":0,"jobId":"9459695","jobNumber":"9927.1"}</field></doc></add>
Python message:
POST /solr/my_coll/update?commit=true HTTP/1.1
Accept-Encoding: identity
Content-Type: application/x-www-form-urlencoded
Content-Length: 138
Host: solr.example.no:12699
User-Agent: Python-urllib/3.7
Connection: close
<add><doc><field name="key">KEY__9927.1</field><field name="value">{"result":0,"jobId":"9459695","jobNumber":"9927.1"}</field></doc></add>
henrik242 commentedon Mar 6, 2020
Root cause for this seems to be https://bugs.python.org/issue12849
henrik242 commentedon Mar 9, 2020
Solved!
The problem was Solr which it has special handling of POSTed data with the User-Agent starts with 'curl/': https://github.com/apache/lucene-solr/blob/40661489cd590947f513e553a20707d0c82b82e5/solr/core/src/java/org/apache/solr/servlet/SolrRequestParsers.java#L782
In all other cases Solr expects the Content-Type to be text/xml. Setting that with urrlib.request makes the request work fine:
A big thanks to https://stackoverflow.com/a/60586102/13365 for figuring this out