Python - Attempt to decode JSON with unexpected mimetype:

pfych picture pfych · Feb 17, 2018 · Viewed 13.2k times · Source

I recently swapped over from requests to aiohttp because I couldn't use it in asyncio loops.

The swap went perfectly and everything goes well except for one thing. My console is full of

Attempt to decode JSON with unexpected mimetype:

and

Attempt to decode JSON with unexpected mimetype: txt/html; charset=utf-8

My code has a list of sites it goes too and grabs JSON from, Each site is different but my loop is basically the same for each of them, Ive simplified it here:

PoolName = "http://website.com"
endpoint = "/api/stats"
headers = "headers = {'content-type': 'text/html'}" #Ive tried "application/json" and no headers
async with aiohttp.get(url=PoolName+endpoint, headers=headers) as hashrate:
                hashrate = await hashrate.json()
endVariable = hashrate['GLRC']['HASH']

It works perfectly, connects to the site grabs the json and sets endVariable correctly. but for some reason

Attempt to decode JSON with unexpected mimetype:

prints every time it goes through the loop. Which is annoying because it prints stats to the console and they get lost in the the errors each time it grabs a sites json

Is there a way to fix this error or to hide it?

Answer

Andrew Svetlov picture Andrew Svetlov · Feb 17, 2018

Pass expected content type to json() method:

data = await resp.json(content_type='text/html')

or disable the check entirely:

data = await resp.json(content_type=None)