can only concatenate str (not "NoneType") to str

11.4k Views Asked by At

Having issues with get api response Text and Print it in python3

response = session.get(url=url, headers=headers, json=payload, verify=cabundle,
                               auth=auth)
        if response.status_code == 200:
            print(response.status_code)
            print("Response is:")
            data = json.loads(response.text)
            print(data)
            if data['issues']!= []:
                issueId = data['issues'][0]['id']
                print("Issue ID :    " +issueId)

Output:

200
Response is:
Expecting value: line 3 column 1 (char 4)
can only concatenate str (not "NoneType") to str

Can anyone help with this problem?

2

There are 2 best solutions below

2
DirtyBit On BEST ANSWER

Check if the issueId exists before concatenating:

issueId = data['issues'][0]['id']
if issueId:
    print("Issue ID : " + issueId)
1
Rashid 'Lee' Ibrahim On

Use string params and it will insert None if it is none type.

print(f"Issue Id: {issueId}")