anonuser0428
anonuser0428

Reputation: 12363

POST Request in Python 'requests' module not working properly

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

is the documentation of the D&B API and hence I am trying to send a POST request using python requests module using the following line of code:

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

the response I am getting is Response [500]

and the content of the response is : error processing request\r\n

My question is whether I am doing something wrong while passing the post request and the parameters or is it a problem with my username and password being invalid?

I feel the problem lies in the way I am passing the POST request as the API responds with a separate error 401 for incorrect userid, 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'}

my response header when I use:

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

A random user-id and password.

But according to the API I am supposed to receive <Response [401]> . I receive <Response [500]> instead.

Upvotes: 4

Views: 9125

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124488

Those are HTTP headers; quoting the 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.

Add them as such:

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

This works for me, albeit that I get a 401 response (as I don't have any valid credentials):

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

entirely as documented.

Upvotes: 5

Related Questions