0

I'm creating delete request to webserver and trying to catch the exceptions like below:

 try: response = req.delete(path, auth=HTTPBasicAuth(config.user(), config.password()), params=params, headers=headers, verify=True) except requests.HTTPError as he: raise SystemExit(he) except requests.ConnectionError as ce: raise SystemExit(ce) except requests.URLRequired as ue: raise SystemExit(ue) except requests.Timeout as te: raise SystemExit(te) except requests.RequestException as err: print('Undefined error: ', err) print(response.status_code) 

But delete is not processed and response.status_code prints 400, but error handling does not work. Any ideas why the error handling is not working there?

1 Answer 1

1

If you want to catch http errors (e.g. 401 Unauthorized) to raise exceptions, you need to call Response.raise_for_status. That will raise an HTTPError, if the response was an http error.

try: response = req.delete(path, auth=HTTPBasicAuth(config.user(), config.password()), params=params, headers=headers, verify=True) except requests.HTTPError as he: raise SystemExit(he) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.