Asynchronous Random User Fetching with aiohttp

  • Share this:

Code introduction


This code defines an asynchronous function to fetch a random user information. It uses the aiohttp library to send an HTTP request to a random user generation API and returns a JSON formatted response.


Technology Stack : aiohttp, asyncio

Code Type : Asynchronous HTTP request

Code Difficulty : Intermediate


                
                    
import aiohttp
import asyncio
import random

def fetch_random_user(session):
    url = 'https://randomuser.me/api/'
    async with session.get(url) as response:
        return await response.json()

async def get_random_user_info():
    async with aiohttp.ClientSession() as session:
        user_info = await fetch_random_user(session)
        return user_info