Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的POST请求';请求';模块工作不正常_Python_Http Post_Python Requests - Fatal编程技术网

Python中的POST请求';请求';模块工作不正常

Python中的POST请求';请求';模块工作不正常,python,http-post,python-requests,Python,Http Post,Python Requests,是D&B API的文档,因此我尝试使用python请求模块发送POST请求,使用以下代码行: POST https://maxcvservices.dnb.com/rest/Authentication x-dnb-user: MyUsername x-dnb-pwd: MyPassword 我得到的响应是response[500] 响应的内容是:错误处理请求\r\n 我的问题是,我是否在传递post请求和参数时出错,还是我的用户名和密码无效 我觉得问题在于我传递POST请求的方式,因为API

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

POST https://maxcvservices.dnb.com/rest/Authentication
x-dnb-user: MyUsername
x-dnb-pwd: MyPassword
我得到的响应是
response[500]

响应的内容是:
错误处理请求\r\n

我的问题是,我是否在传递post请求和参数时出错,还是我的用户名和密码无效

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

r = requests.post('https://maxcvservices.dnb.com/rest/Authentication',params={'x-dnb-user':userid,'x-dnb-pwd':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'}
随机用户id和密码

但是根据API,我应该收到

我收到的是

这些是HTTP头;引述:

通过使用身份验证令牌来管理对D&B Direct services的安全访问,可以通过向身份验证服务URL发送HTTP POST请求、在HTTP头中传递有效的用户名和密码来获得身份验证令牌

添加如下内容:

r = requests.post('https://maxcvservices.dnb.com/rest/Authentication', headers={'x-dnb-user': 'userid', 'x-dnb-pwd': 'password'})
这对我来说很有效,尽管我得到了401响应(因为我没有任何有效的凭证):

导入请求 >>>请求。\u版本__ '2.0.0' >>>r=请求。post('https://maxcvservices.dnb.com/rest/Authentication', …标题={'x-dnb-user':'userid','x-dnb-pwd':'password'}) >>>r >>>r.headers[“授权”] “无效凭据”
完全如文档所示。

我尝试过,但仍然收到相同的“错误处理请求”消息。回应500@user1009091:您确定您正确地调用了此选项吗?使用随机用户名和密码值,我得到了401响应。这很奇怪,我做了完全相同的事情,不仅我得到了,我的响应头中甚至没有授权密钥。你安装了什么版本的
requests
<代码>请求。我的版本为2.0。您是否尝试了与我的示例会话中发布的代码完全相同的代码?那么使用
'userid'
'password'
字符串文本?
r = requests.post(
    'https://maxcvservices.dnb.com/rest/Authentication',
    headers={'x-dnb-user': userid, 'x-dnb-pwd': password})
>>> 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'