尝试使用python中的请求访问REST API时出现错误请求(400)错误

时间:2013-06-11 22:22:07

标签: python post python-requests

这个错误是如此常见,以至于我发现了这个错误的几个点击。但是,找不到我的问题的解决方案。

我正在尝试使用Python中的请求模块来使用REST API(不是公开的API)。但是,API使用的规范表明我使用URI Content-type = 'application/xml'和正文"<authCommand><userID> </userID><password> </password></authCommand>"调用POST方法。

我在Python中使用请求尝试了这个: r1 = requests.post('https://abc.360.net/rest/auth/1/login.xml',data= None, auth=('sdm@company.com','*********'), headers = {'content-type':'application/xml'})

当我运行它时,我收到错误的请求错误。

这是正确的做法吗?为什么我收到此错误?

1 个答案:

答案 0 :(得分:0)

auth参数是设置Authorization 标头(默认情况下是基本的http身份验证)。 API需要正文中的凭据:

r = request.post(url, data=xml, headers={'Content-Type': 'application/xml'})

其中xml=b"<authCommand><userID>sdm@company.com</authCommand>..."