簡體   English   中英

Python'請求'模塊中的POST請求無法正常工作

[英]POST Request in Python 'requests' module not working properly

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]Response [500]

並且響應的內容是: error processing request\\r\\n

我的問題是,在傳遞帖子請求和參數時我是否做錯了,還是我的用戶名和密碼無效?

我覺得問題在於我傳遞POST請求的方式,因為API響應錯誤的用戶ID,傳遞單獨的錯誤401。

{'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]>

這些是HTTP標頭; 引用API文檔

通過使用身份驗證令牌來管理對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'

完全如文件所述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM