close

[Solved] Python – Attempt to decode JSON with unexpected mimetype:

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python – Attempt to decode JSON with unexpected mimetype: in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python – Attempt to decode JSON with unexpected mimetype: Error Occurs?

Today I get the following error Python – Attempt to decode JSON with unexpected mimetype: in python.

How To Solve Python – Attempt to decode JSON with unexpected mimetype: Error ?

  1. How To Solve Python – Attempt to decode JSON with unexpected mimetype: Error ?

    To Solve Python – Attempt to decode JSON with unexpected mimetype: Error 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.

  2. Python – Attempt to decode JSON with unexpected mimetype:

    To Solve Python – Attempt to decode JSON with unexpected mimetype: Error 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.

Solution 1

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.

Solution 2

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)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read