问题
how can i post only key of form data to a server like the site itself?
In [1]: from requests import Request
In [2]: req = Request('POST', 'http://www.google.com', data={'json':''}).prepare()
In [3]: req.headers, req.body
Out[3]:
({'Content-Length': '5', 'Content-Type': 'application/x-www-form-urlencoded'},
'json=')
In [4]: req = Request('POST', 'http://www.google.com', data={'json':None}).prepare()
In [5]: req.headers, req.body
Out[5]: ({'Content-Type': 'application/x-www-form-urlencoded'}, '')
the 'Content-Length' can't equal length of key, like 4. and, how can i make the body equal 'json' ? thx all
回答1:
From the documentation:
There are times that you may want to send data that is not form-encoded. If you pass in a
string
instead of adict
, that data will be posted directly.
In your case, that means doing this:
req = Request('POST', 'http://www.google.com', data='json').prepare()
来源:https://stackoverflow.com/questions/59572581/python-requests-post-data-without-value-dont-work