title: How to read lines of a streaming api with aiohttp in Python?
body: <p>I am trying to convert a Python HTTP client featuring <code>requests</code> to <code>aiohttp</code>. The logic is to send a GET call to a REST endpoint which streams data occasionally and print the lines it returns.</p>
  
  <p>I have a code using <a href="https://requests.readthedocs.io/en/master/" rel="nofollow noreferrer">requests</a> with <code>stream=True</code> option and <code>iter_lines</code>, it works pretty fine:</p>
  
  <pre><code>import json
  import requests
  
  def main():
      with requests.get('https://my-streaming-url.com', stream=True) as r:
          if r.encoding is None:
              r.encoding = 'utf-8'
          for line in r.iter_lines(decode_unicode=True):
              if line:
                  # Print each line emitted by the streaming api
                  print(json.loads(line))
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>Now, I want to convert this logic to <a href="https://docs.aiohttp.org/en/stable/streams.html#asynchronous-iteration-support" rel="nofollow noreferrer">aiohttp streaming api</a> and tried:</p>
  
  <pre><code>import asyncio
  import aiohttp
  import json
  
  loop = asyncio.get_event_loop()
  
  async def main():
      r = aiohttp.request('get', 'https://my-streaming-url.com')
      async for line in r.content:
          print(json.loads(line))
  
  if __name__ == '__main__':
      loop.run_until_complete(connect_and_listen())
      loop.close()
  </code></pre>
  
  <p>I get an error like:</p>
  
  <blockquote>
    <p>... in connect_and_listen</p>
    
    <p>async for line in r.content:</p>
    
    <p>AttributeError: '_SessionRequestContextManager' object has no attribute 'content'</p>
    
    <p>sys:1: RuntimeWarning: coroutine 'ClientSession._request' was never awaited</p>
    
    <p>Unclosed client session</p>
    
    <p>client_session: aiohttp.client.ClientSession object at 0x7fac6ec24310</p>
  </blockquote>
  
  <p>I tried a few ways like removing <code>loop.close()</code> from <code>main</code>, removing <code>async</code> from the <code>for</code> loop, but none helped.</p>
  
  <p>What am I missing here? How can I print a streaming api lines with <code>aiohttp</code>?</p>
  
  <p>P.S: My Python version is 3.7.5</p>