HTTP requests.post失败

时间:2013-10-11 01:45:03

标签: python python-3.x python-requests

我正在使用python请求库来获取和发布http内容。我使用get函数没有问题,但我的post函数似乎失败或根本没有做任何事情。根据我对请求库的理解,POST函数会自动对您发送的数据进行编码,但我不确定这是否真的发生了

代码:

data = 'hash='+hash+'&confirm=Continue+as+Free+User'   
r = requests.post(url,data)
html = r.text

通过检查html的“值”我可以告诉返回响应是没有POST的url的响应。

3 个答案:

答案 0 :(得分:3)

您没有利用请求如何为您编码。为此,您需要以这种方式编写代码:

data = {'hash': hash, 'confirm': 'Continue as Free User'}
r = requests.post(url, data)
html = r.text

我无法为你测试,但这就是编码自动发生的方式。

答案 1 :(得分:0)

post(url, data=None, **kwargs)
Sends a POST request. Returns :class:`Response` object.

:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.

答案 2 :(得分:0)

import requests

url = "http://computer-database.herokuapp.com/computers"

payload = "name=Hello11111122OKOK&introduced=1986-12-26&discontinued=2100-12-26&company=13"
headers = {
    'accept-language': "en-US,en;q=0.9,kn;q=0.8",
    'accept-encoding': "gzip, deflate",
    'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'content-type': "application/x-www-form-urlencoded",
    'cache-control': "no-cache",
    'postman-token': "3e5dabdc-149a-ff4c-a3db-398a7b52f9d5"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)