You can download this code by clicking the button below.
This code is now available for download.
This function uses aiohttp's ClientSession to asynchronously make a request to a random user information API and prints the returned data.
Technology Stack : aiohttp, asyncio
Code Type : Asynchronous HTTP client request
Code Difficulty : Intermediate
import aiohttp
import asyncio
async def fetch_random_user(session):
url = 'https://randomuser.me/api/'
async with session.get(url) as response:
return await response.json()
def main():
loop = asyncio.get_event_loop()
async def run():
async with aiohttp.ClientSession() as session:
user_data = await fetch_random_user(session)
print(user_data)
loop.run_until_complete(run())
if __name__ == '__main__':
main()