0

I used the following script to make an API call with credentials from this thread

import requests

username = 'johndoe'
password= 'zznAQOoWyj8uuAgq'

headersAuth = {
    'accept': 'application/json',
    'Content-length': ''
}

data = {'username': username,'password': password}

## Authentication request

response = requests.post('https://somedomain.test.com/token', headers=headersAuth, data=data, verify=True)
j = response.json()
print(j)

headersAPI = {
    'accept': 'application/json',
    'Content-length': '',
    'Authorization': j['access_token']
}

# Making sample API call with authentication and API parameters data

response = requests.get('https://somedomain.test.com/api/Users/Year/2020/Workers', headers=headersAPI, verify=True)
api_response = response.json()

And when I print j, I get None. The output is:

    j = response.json()
AttributeError: 'str' object has no attribute 'json'

How can I solve this error?

Josh
  • 1
  • 1
  • Please do _never_ include real credentials in your questions, even if the endpoint was hidden here. – ahuemmer Aug 05 '22 at 12:14

1 Answers1

0

In order for us to help more also include error outputs as well. You might be requesting wrong or sending wrong parameters.

Try putting raise_status like below before your response line;

response = requests.post('https://somedomain.test.com/token', headers=headersAuth, data=data, verify=True)

response.raise_for_status()

j = response.json()

This way you can check if your response is valid or not.

Lanre
  • 340
  • 2
  • 6
  • Thanks Lanre, nothing is returned, so it seems OK. Maybe it could be the Content-Length which is not set? – Josh Aug 04 '22 at 15:30
  • 1
    You can also print result of it for status with ; print(response.raise_for_status() – Lanre Aug 05 '22 at 07:21
  • 1
    And since there is problem with your variable j; AttributeError: 'str' object has no attribute 'json' . It seems there is no json data returning. I advice you to check your get request if its aligning with api documentation. – Lanre Aug 05 '22 at 07:23