【问题标题】:POST Request in Python 'requests' module not working properlyPython“请求”模块中的 POST 请求无法正常工作
【发布时间】:2013-10-26 17:29:04
【问题描述】:
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 请求和参数时是否做错了,还是我的用户名和密码无效?

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

{'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'})

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

但根据 API 我应该收到 <Response [401]> 。 我收到的是<Response [500]>

【问题讨论】:

    标签: python http-post python-requests


    【解决方案1】:

    这些是 HTTP 标头;引用API documentation:

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

    像这样添加它们:

    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'
    

    完全按照文档记录。

    【讨论】:

    • 我试过了,但仍然收到相同的“错误处理请求”消息。响应 500
    • @user1009091:你确定你这样称呼正确吗?使用随机的用户名和密码值,我会收到 401 响应。
    • 这很奇怪,我做了同样的事情,不仅得到了 ,我的响应头甚至没有授权密钥。
    • 您安装了哪个版本的requestsrequests.__version__ 对我来说是 2.0。
    • 您是否尝试过与我的示例会话中发布的完全相同的代码?那么使用'userid''password' 字符串文字?
    猜你喜欢
    • 1970-01-01
    • 2021-04-10
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2014-11-03
    相关资源
    最近更新 更多