0

I am in trouble using aiohttp Python module: in some cases (not always, no patterns, just happens randomly), the get method produce an unexpected URI...

My client code is something like this (the URI is dummy):

 # Python 3
 import aiohttp, async

 async with aiohttp.ClientSession() as session:

       URI = 'https://apiprovider.io/route?paran1=hi&key=hsaf4ed8f4ad6f874asd'

       async with session.get(URI) as response:
              result_data = await response.json()

The error message (not allways, but randomly):

 aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with 
 unexpected mimetype: ', url=URL('https://apiprovider.io/route?paran1=hi&key=hsaf4ed8f4ad6f874asd')

Python accuses an "decode JSON" error, but it is just a consequence of the real problem.

The server response is: {"status":"ERROR","request_id":"random_numb","error":"Unknown API Key"}

The key "error":"Unknow API key" is caused because the module (or something in the way) change de URI.

The original URI becames https://apiprovider.io/route?paran1=hi&key=hsaf4ed8f4ad6f874asd'

Please, note the single cote in the end of the string, it seems to became part of the URI itself... exactly in the "key" param.

If I use the same URI in my browser, just removing the single cote in the end, the result is correct.

Can someone explain to me why this happens and how can I fix it?

I already try to change the order of URI params, and also try to use the "params" param in the get method like this:

 [...]
 async with session.get(URI, params={'key':key, 'param1'=hi}) as response:
 [...]

None of this works... The erros stops for a while, but come back eventualy.

Thank you very much!

  • Why do you think there's a single quote at the end of the URL? The error message says: `URL('https://the_url')` - both single quotes are part of the string representation of the URL and aren't actually present _in_ the string – ForceBru Aug 17 '21 at 22:22
  • Thanks a lot for the comment! I'm suggesting the error is in sigle cote because if I request the same URL in my browser, I receive the server error that my "key" arg was incorret (which is not)... but whan I remove the single cote, the error is fixed... so simple as backspace it... note: the single cote is "attached" to URL just when the error is rised, which is the one I used to test in my browser (I copy the link produced by the error and past it into my browser). – joe_counter Aug 18 '21 at 00:00

1 Answers1

0

The error message says that your error is due to a JSON decode error resulting from an incorrect mimetype, which basically means the API is returning a response that looks like JSON but is not flagged as JSON by the server. More detail explaining this in this answer.

As someone else has flagged in a comment, it is also therefore not because your url is being changed.

You can disable this behaviour in your code by changing it as below, which will prevent a check of the mimetype.

async with session.get(URI) as response:
              result_data = await response.json(content_type=None)
osint_alex
  • 952
  • 3
  • 16