urllib3 not throwing exception on 400 Bad Request response from endpoint

75 Views Asked by At

I'm trying to make a request and handle any errors thrown by an endpoint using urllib3

My code and expectations are as follows:

import urllib3
...
try:
  response = http.request('POST', url, headers=headers, body=encoded_data)
  print(response.status) # => 400

except urllib3.exceptions.HTTPError as e:
  print(e) # Expect this to be reached but isn't

except Exception as e:
  print(e) # This also isn't hit telling me an exception isn't thrown at all

I've purposefully malformed my request to throw a 400 and expected urllib3 to handle the request similarly to urllib:

from urllib import request, error
...
try:
  response = request.urlopen(request, data=encoded_data)
  content = response.read()

except error.HTTPError as e:
  print(e) # This is hit in the case of a 400+ response
0

There are 0 best solutions below