python - Python 'requests' 模块中的 POST 请求无法正常工作

标签 python http-post python-requests

POST https://maxcvservices.dnb.com/rest/Authentication
x-dnb-user: MyUsername
x-dnb-pwd: MyPassword

是 D&B API 的文档,因此我尝试使用以下代码行使用 python 请求模块发送 POST 请求:

r = requests.post('https://maxcvservices.dnb.com/rest/Authentication',params={'x-dnb-user':userid,'x-dnb-pwd':pass})

我得到的响应是 Response [500]

响应的内容是:error processing request\r\n

我的问题是我在传递发布请求和参数时是否做错了什么,还是我的用户名和密码无效?

我觉得问题在于我传递 POST 请求的方式,因为 API 响应一个单独的错误 401 错误的用户 ID,pass。

{'connection': 'Keep-Alive',
 'content-encoding': 'gzip',
 'content-length': '46',
 'content-type': 'text/plain',
 'date': 'Sat, 26 Oct 2013 17:43:22 GMT',
 'server': '',
 'vary': 'Accept-Encoding',
 'x-correlationid': 'Id-d4c07ad3526bff3a03fb742e 0'}

我使用时的响应头:

r = requests.post('https://maxcvservices.dnb.com/rest/Authentication', headers={'x-dnb-user': 'userid', 'x-dnb-pwd': 'password'})

一个随机的用户名和密码。

但根据 API,我应该收到 <Response [401]> . 我收到 <Response [500]>相反。

最佳答案

那些是 HTTP header ;引用 API documentation :

Secure access to D&B Direct services is managed through the use of an Authentication Token, which can be obtained by sending an HTTP POST request to Authentication Service URL, passing a valid username and password in the HTTP header.

这样添加它们:

r = requests.post(
    'https://maxcvservices.dnb.com/rest/Authentication',
    headers={'x-dnb-user': userid, 'x-dnb-pwd': password})

这对我有用,尽管我收到 401 响应(因为我没有任何有效凭据):

>>> import requests
>>> requests.__version__
'2.0.0'
>>> r = requests.post('https://maxcvservices.dnb.com/rest/Authentication',
...                   headers={'x-dnb-user': 'userid', 'x-dnb-pwd': 'password'})
>>> r
<Response [401]>
>>> r.headers['authorization']
'INVALID CREDENTIALS'

完全如文档所述。

关于python - Python 'requests' 模块中的 POST 请求无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19609703/

相关文章:

Python:请求 session 登录 Cookie

python - 无法通过python登录网站

python - 无法对符合给定条件的列中的值求和

python - Shiboken无法导入

Python 数据库游标异常时序

python - 如何获取谷歌 "fast answer box"文本?

python - 如何将类添加到 Django 国家/地区小部件

android - 在android中使用http post发送位图的字节数组?

java - 带有负载的 Jsoup HTTP POST

java - Elasticsearch 中的 JSON 后编码