get status_code for http.urlopen

39 Views Asked by At

How can I print the status code for this response? (eg 200/401 etc)

resp = http.urlopen('POST', 'https://api.bitbucket.org/2.0/repositories/6789oh', headers=headers, body=json.dumps(data))
print(str(resp.data))

I tried:

resp.code
resp.get_code()

etc but none of them work for http.urlopen.

1

There are 1 best solutions below

0
Akukwe Ebube On

I would use urllib.request , try this see if it helps

import urllib.request , json

try:

resp=urllib.request.urlopen('POST','https://api.bitbucket.org/2.0/repositories/6789oh',headers=headers, body=json.dump(data))

except urllib.error.URLError as a :

print(str(a.status) +"\n" + str(a.message))

except urllib.error.URLError as e :

print(str(e.code) +"\n"+ str(e.msg))