Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Attempt to decode JSON with unexpected mimetype:

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?

like image 839
pfych Avatar asked Feb 17 '18 10:02

pfych


2 Answers

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)
like image 68
Andrew Svetlov Avatar answered Nov 10 '22 01:11

Andrew Svetlov


aiohttp is trying to do the right thing and warn you of incorrect Content-Type, which could at worst indicate that you are not getting JSON data at all, but something unrelated, such as the HTML content of an error page.

However, in practice many servers are misconfigured to always send the incorrect MIME type in their JSON responses, and JavaScript libraries apparently don't care. If you know you're dealing with such a server, you can always silence the warning by invoking json.loads yourself:

import json
# ...

async with self._session.get(uri, ...) as resp:
    data = await resp.read()
hashrate = json.loads(data)

Specifying Content-Type as you attempted makes no difference because it only affects the Content-Type of your request, whereas the problem lies in the Content-Type of the server's response, which is not under your control.

like image 27
user4815162342 Avatar answered Nov 10 '22 02:11

user4815162342