refs #702 have RESTApi.py handle errors and non-json responses

pull/9/head
Natalia Serrano 2024-09-13 12:35:15 +02:00 committed by Natalia Serrano
parent 07a8a5b4af
commit 178d724ec0
1 changed files with 4 additions and 0 deletions

View File

@ -142,6 +142,10 @@ class REST(object):
else:
r = requests.post(url, data=data, headers={'content-type': 'application/json'})
r.raise_for_status()
ct = r.headers['Content-Type']
if 'application/json' != ct:
raise Exception (f'response content-type is not "application/json" but "{ct}"')
r = json.loads(r.content) # Using instead of r.json() to make compatible with old requests lib versions
except requests.exceptions.RequestException as e:
raise ConnectionError(e)