Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass headers to async with session.get()

I would like to know how to pass the header in the following get call

headers = {
'User-Agent': 'Mozilla'
}
async def fetch(url, session):
async with session.get(url) as response:
    resp = await response.read()
    return resp

I tried the following but not getting any response.

headers = {
'User-Agent': 'Mozilla'
}
async def fetch(url, session):
async with session.get(url, headers=headers) as response:
    resp = await response.read()
    return resp

The objective is to call different urls in asynchronous mode. Need to know if there is any other alternate way as well but in any case, would need to pass the headers to get proper response.

like image 228
Akhil Pakkath Avatar asked Sep 19 '25 20:09

Akhil Pakkath


1 Answers

You can use httpbin.org for requests to see how servers sees your request:

import asyncio
import aiohttp
from pprint import pprint


headers = {
    'User-Agent': 'Mozilla'
}


async def fetch(url, session):
    async with session.get(url, headers=headers) as response:
        res = await response.json()
        pprint(res)


async def main():
    async with aiohttp.ClientSession() as session:
        await fetch("http://httpbin.org/get", session)


asyncio.run(main())

Result:

{'args': {},
 'headers': {'Accept': '*/*',
             'Accept-Encoding': 'gzip, deflate',
             'Host': 'httpbin.org',
             'User-Agent': 'Mozilla',
             'X-Amzn-Trace-Id': 'Root=1-602f94a7-3aa49d8c48ea04345380c67b'},
 'origin': '92.100.218.123',
 'url': 'http://httpbin.org/get'}

As you see 'User-Agent': 'Mozilla' was sent.

like image 187
Mikhail Gerasimov Avatar answered Sep 23 '25 11:09

Mikhail Gerasimov