python requests.post data without value don't work

拟墨画扇 提交于 2021-02-10 20:08:58

问题


how can i post only key of form data to a server like the site itself?

http_post_text_or_only_key_of_dict

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 a dict, 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!