python请求。没有值的post数据不存在

2024-04-26 04:07:58 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将表单数据的唯一键发布到像站点本身这样的服务器上

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'}, '')

“Content Length”不能等于键的长度,如4。 那么,如何使body等于“json”?全部thx


Tags: incomjsonhttpdatarequestwwwgoogle
1条回答
网友
1楼 · 发布于 2024-04-26 04:07:58

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.

在您的情况下,这意味着:

req = Request('POST', 'http://www.google.com', data='json').prepare()

相关问题 更多 >