python - 在Python中使用 'requests'模块发出POST请求

标签 python post httprequest python-requests

所以我已经安装了 python 'requests' 模块,并且它正在工作,但我无法看到它是如何组合在一起的。我在网上搜索过,但找不到任何有关此功能如何工作的实际示例。

因此,我将提供一个示例 POST 请求,而我正在寻找的答案是 Python 代码中的该请求。我到处寻找,但找不到基本 POST 格式与 Python 中的格式之间的任何直接转换。

感谢您的帮助!

这是我的 POST 请求示例:

POST / HTTP/1.1
content-type:application/x-www-form-urlencoded;charset=utf-8
host: https://testsite.com
content-length:207

Blah=content&blah2=content2

最佳答案

使用 .post() 函数将字典传递给 data 关键字:

data = {'Blah': 'content', 'blah2': 'content2'}
r = requests.post('https://testsite.com/', data=data)

请参阅More complicated POST requests快速入门文档部分。

同一篇文章http://httpbin.org/post :

>>> import requests, pprint
>>> data = {'Blah': 'content', 'blah2': 'content2'}
>>> r = requests.post('http://httpbin.org/post', data=data)
>>> pprint.pprint(r.json())
{u'args': {},
 u'data': u'',
 u'files': {},
 u'form': {u'Blah': u'content', u'blah2': u'content2'},
 u'headers': {u'Accept': u'*/*',
              u'Accept-Encoding': u'gzip, deflate, compress',
              u'Connection': u'close',
              u'Content-Length': u'27',
              u'Content-Type': u'application/x-www-form-urlencoded',
              u'Host': u'httpbin.org',
              u'User-Agent': u'python-requests/2.0.1 CPython/2.7.5 Darwin/11.4.2',
              u'X-Request-Id': u'6df0a100-f193-4272-adf5-3b6bb6a77461'},
 u'json': None,
 u'origin': u'84.92.98.170',
 u'url': u'http://httpbin.org/post'}
>>> r.request.headers
CaseInsensitiveDict({'Content-Length': u'27', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept-Encoding': 'gzip, deflate, compress', 'Accept': '*/*', 'User-Agent': 'python-requests/2.0.1 CPython/2.7.5 Darwin/11.4.2'})

httpbin.org 回显已发送的 header ; Content-LengthContent-Type header 已由 requests 为您设置。

您始终可以使用 headers 参数添加或覆盖 header :

>>> r = requests.post('http://httpbin.org/post', data=data,
...     headers={'User-Agent': 'Stack Overflow requests demo'})
>>> pprint.pprint(r.json())
{u'args': {},
 u'data': u'',
 u'files': {},
 u'form': {u'Blah': u'content', u'blah2': u'content2'},
 u'headers': {u'Accept': u'*/*',
              u'Accept-Encoding': u'gzip, deflate, compress',
              u'Connection': u'close',
              u'Content-Length': u'27',
              u'Content-Type': u'application/x-www-form-urlencoded',
              u'Host': u'httpbin.org',
              u'User-Agent': u'Stack Overflow requests demo',
              u'X-Request-Id': u'b489f151-e5c2-4f49-ad00-141a0658c54a'},
 u'json': None,
 u'origin': u'84.92.98.170',
 u'url': u'http://httpbin.org/post'}
>>> r.request.headers
CaseInsensitiveDict({'Content-Length': u'27', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept-Encoding': 'gzip, deflate, compress', 'Accept': '*/*', 'User-Agent': 'Stack Overflow requests demo'})

关于python - 在Python中使用 'requests'模块发出POST请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21505716/

相关文章:

php - 以编程方式提交 WordPress 表单密码

asp.net-core - 将 POST 请求从 asp.net 核心 Controller 转发到不同的 URL

android - 如何检查 WebView 发出的 HTTP 请求

python - Django-nose 奇怪的输出

python - sage 5.0 execfile() 语法错误

python - 要求 python 导入是模块

c# - 如何使用 HttpClient 发布表单数据 IFormFile?

javascript - Postman form-data 和 x-www-form-urlencoded 可以工作,但原始 json 不行

c# - 在没有 HTTP 请求的情况下使用 PerRequestLifetimeManager 解析类型

Python 在 Windows 上的 os.listdir 行为