Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.4K
Views
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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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)
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!