Python请求模块不使用post方法

标签 python forms http python-requests python-3.3

我正在尝试将表单数据发布到 URL。我没有得到预期的响应,并对从请求模块(2.6.2)获得的一些信息感到好奇。以下是post方法:

>>> response = requests.post(url, data={'uname':user, 'pwd':password,'phrase':'','submit':True})

如您所见,我使用的是 post() 方法,因此我希望该方法为 POSTdata 对象的键与表单元素的名称匹配。 URL 是表单操作。

>>> vars(response.request)
{'method': 'GET', 'body': None, '_cookies': <<class 'requests.cookies.RequestsCookieJar'>[]>, 'headers': {'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'python-requests/2.6.2 CPython/3.3.6 Windows/8', 'Connection': 'keep-alive'}, 'hooks': {'response': []}, 'url': url}

response.request 属性应包含有关为此响应发送的请求的信息。它的 method 属性是 GET,我期望的是 POST。该网址看起来正确。如果表单发布,页面预计会返回其他内容,奇怪的是,我会检查请求 header 。

>>> response.request.headers
{'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'python-requests/2.6.2 CPython/3.3.6 Windows/8', 'Connection': 'keep-alive'}
>>> 

这些看起来都不错,只是等一下!我的表单数据在哪里?为什么它不与请求一起发送?我检查历史记录,发现我被重定向了。这次我将 allow_redirects=False 添加到我的 post() 调用中。然后检查 response.request 对象及其 header 。

>>> vars(response.request)
{'method': 'POST', 'body': 'phrase=&pwd=****&uname=****&submit=True', '_cookies': <<class 'requests.cookies.RequestsCookieJar'>[]>, 'headers': {'Accept-Encoding': 'gzip, deflate', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'python-requests/2.6.2 CPython/3.3.6 Windows/8', 'Content-Length': '56', 'Accept': '*/*', 'Connection': 'keep-alive'}, 'hooks': {'response': []}, 'url': 'http://myurl.com/path/to/script.php'}

这次是一个POST,所以看起来是正确的。我觉得我走在正确的轨道上。这很奇怪,body 属性看起来像一个查询字符串,而不是我期望的表单帖子。

>>> response.request.headers
{'Accept-Encoding': 'gzip, deflate', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'python-requests/2.6.2 CPython/3.3.6 Windows/8', 'Content-Length': '56', 'Accept': '*/*', 'Connection': 'keep-alive'}

这些 header , header 中同样没有表单数据。这是什么? '内容类型':'application/x-www-form-urlencoded'?这就是为什么我的表单数据看起来像查询字符串的原因吗?正常的内容类型是什么?它与 Chrome 报告的访问相同 URL 的类型相同,因此我怀疑这就是问题所在。

如果这些都没有问题,他们可能很聪明,会拒绝来自非本地来源的帖子,对吧?我主要担心的是表单数据是 body 属性中的字符串,这似乎是错误的。如果没有,我可以聪明地设置 HTTP header 来源吗?

最佳答案

'application/x-www-form-urlencoded'

这是default content-type发布您的数据时。

是的,如果您的 POST 数据看起来像查询字符串也没关系,这就是使用 x-www-form-urlencoded 发送的方式。来自上一个链接:

The control names/values are listed in the order they appear in the document. The name is separated from the value by = and name/value pairs are separated from each other by &.

下一个:

Those headers, again no form data in the header

使用 POST 时,表单数据不会在 header 中发送。它在请求正文中发送。尝试一下

>>> response.request.body
'uname=x&pwd=y&submit=True&phrase='

查看此数据

关于Python请求模块不使用post方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29839499/

相关文章:

python - QObject::connect: 无法对类型为 'QTextCursor' 的参数进行排队

javascript - 比较数组图像和值,如果可以提交表单

http - Rebol2 的读取/自定义文档?

python - 获取时间日期范围

python - 在气隙机器上的 Anaconda 中安装非 conda 包

python - Webdriver 测试 - 将事件/通知推送到测试

javascript - JS/JQuery : Dynamically add "pattern" and "title" attribute to form input

php - 如何创建具有双 GET 变量的数组

sockets - python3 上的 Tornado

java - 为什么从套接字流读取会永远阻塞?