Skip to content

response.json() incorrectly assumes some json responses are text. (from github raw links to json files) #1766

@AraHaan

Description

@AraHaan

Long story short

Some json files (those on github and requested by their raw links) are assumed to be text when they are really json.

Expected behaviour

aoihttp to correctly identify the data in the raw github url is in fact json.

Actual behaviour

Some json files (those on github and requested by their raw links) are assumed to be text when they are really json.

Steps to reproduce

  1. create an github repo with json files.
  2. get raw links to them.
  3. use ClientSession.get on them.
  4. try to use r.json() on the response.

Your environment

Python 3.6.0rc1
Windows 7 Ultimate SP1 build 7601

Example that can be reporduced

version that fails:

import aiohttp
import asyncio


class HTTPClient:
    """test HTTP client."""
    def __init__(self, loop=None):
        self.loop = loop if loop is not None else asyncio.get_event_loop()
        self.session = aiohttp.ClientSession(loop=self.loop)

    @asyncio.coroutine
    def request(self, *args, **kwargs):
        yield from self.session.request(*args, **kwargs)

    @asyncio.coroutine
    def get(self, *args, **kwargs):
        data = yield from self.session.get(*args, **kwargs)
        return data

    def close(self):
        self.session.close()


async def get_json(loop=None):
    session = HTTPClient(loop=loop)
    data = await session.get("https://raw.githubusercontent.com/AraHaan/DecoraterBot-Plugins/master/pluginlist.json")
    r = await data.json()  # aiohttp fails here.
    print(r)
    session.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(get_json(loop=loop))

version that does not complain but is of incorrect type for me (not an json object that I would need to get):

import aiohttp
import asyncio


class HTTPClient:
    """test HTTP client."""
    def __init__(self, loop=None):
        self.loop = loop if loop is not None else asyncio.get_event_loop()
        self.session = aiohttp.ClientSession(loop=self.loop)

    @asyncio.coroutine
    def request(self, *args, **kwargs):
        yield from self.session.request(*args, **kwargs)

    @asyncio.coroutine
    def get(self, *args, **kwargs):
        data = yield from self.session.get(*args, **kwargs)
        return data

    def close(self):
        self.session.close()


async def get_json(loop=None):
    session = HTTPClient(loop=loop)
    data = await session.get("https://raw.githubusercontent.com/AraHaan/DecoraterBot-Plugins/master/pluginlist.json")
    r = await data.text()
    print(r)
    session.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(get_json(loop=loop))

Maybe what there needs to be is something to check if the response data looks to be json and if so make it so I can use r.json() on the response.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions