0

I'm in the process of learning python asynchrony. My problem is that I am trying to get an answer in the form of streaming content, but as I did not try, empty bytes always come. What is the reason? What am I doing wrong? I chose the Github API as an example.

I use version python 3.8, aiohttp 3.7.4.

Here is my code: import json import aiohttp import asyncio async def get_response(): url = 'https://api.github.com/events' async with aiohttp.ClientSession() as session: task1 = asyncio.create_task(make_request(session, url)) result = await asyncio.gather(task1) return result

async def make_request(session, url):
    async with session.get(url) as resp:
        json_resp = await resp.json(loads=json.loads)
        bytes_resp = await resp.content.read(10)
        print(json_resp)
        print(bytes_resp)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(get_response())

Many thanks.

Getting this enter image description here

4
  • I had to correct some indentation in your code, but i managed to get a response from 'api.github.com/events' of b'[{"id":"17', currently using aiohttp 3.7.4 and python 3.8. do you just get a response of b''? Commented Aug 1, 2021 at 10:21
  • Yes. but if I remove json_resp from my code, then everything is ok Commented Aug 1, 2021 at 10:28
  • Do you need to add import json to the top? I just ran it and the json_resp printed out the whole response from the url. Commented Aug 1, 2021 at 10:30
  • I accidentally forgot to add this to the code used in the question.I'll fix it now Commented Aug 1, 2021 at 10:33

1 Answer 1

3

You can't both read the response once as JSON and again as bytes, as it is already consumed at that point. (aiohttp purposely doesn't buffer the response data internally.)

If you need both,

bytes_resp = await resp.content.read()
json_resp = json.loads(bytes_resp)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. Everything worked for me. And can I somehow make the most of the possibilities of StreamReader?
Totally depends on what you want to do. That events JSON endpoint isn't a live stream, if that's what you're going for...
I would like to be able to iterate asynchronously on the received response.
Then you'd just need a loop that does smaller reads from the response stream :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.