I'm using Python 3.6 and the aiohttp library to make an API Post request to a server. If I use the wrong username when making the request, I get an HTTP 403 error as I expect. When I make this request in Postman, the body of the response shows:
{"error_message": "No entitlements for User123"}
When I make the request using aiohttp however, I don't see this response body anywhere. The message just says "Forbidden". How can I get the error message above in my Python code?
Edit: Here's my aiohttp code, although it's pretty straightforward:
try:
async with self.client_session.post(url, json=my_data, headers=my_headers) as response:
return await response.json()
except ClientResponseError as e:
print(e.message) # I want to access the response body here
raise e
Edit 2: I found a workaround. When I'm creating the client_session
, I set the raise_for_status value to False. Then when I get a response from the API call, I check if the status is >= 400. If so, I handle the error myself, which includes the body of the response.
Edit 3: Here's the code for my workaround:
self.client_session = ClientSession(loop=asyncio.get_event_loop(), raise_for_status=False)
####################### turn off the default exception handling ---^
try:
async with self.client_session.post(url, json=my_data, headers=my_headers) as response:
body = await response.text()
# handle the error myself so that I have access to the response text
if response.status >= 400:
print('Error is %s' % body)
self.handle_error(response)