aiohttp_retry
aiohttp_retry copied to clipboard
Retries on aiohttp.client_exceptions.ContentTypeError Exception
I am trying to implement retries for the following exception:
aiohttp.client_exceptions.ContentTypeError: 0, message = 'Unexpected attempt to decode JSON with mimetype:'
for that I use this retry options:
from aiohttp.client_exceptions import ContentTypeError
retry_options = RandomRetry (attempts = 5, exceptions = {ContentTypeError})
but I getting the same error, I don't know if there is any way to know if this happens even after retries or that it could be wrong
Thanks
That's why you will still use the client but instead of doing
data = await resp.json() as it attempts to get a json formatted response but the server sometimes decides to pass it with a different content/mime type, it's not a fault on your end.
You'll do this.
data = await resp.read()
data = json.loads(data)
@jsguerrero hello!
It seems that @outgaze response is right. You got response, but it's malformed. There may be incorrect json or just plain text. Anyway you got the response successfully. aiohttp-retry designed to cover only network related errors. But actually there no errors in those terms, you probably received 2xx response.
However I'm thinking of adding post response validation. It maybe fix your problem and should not be hard to do. Stay tuned
You can now use evaluate_response_callback to check if the body is a correct json. Look at this commit, there is an example in tests https://github.com/inyutin/aiohttp_retry/commit/5f51f6aa248b4a0d86e80b7a8ecf147d6f6e70b3