You can download this code by clicking the button below.
This code is now available for download.
This code defines an asynchronous function `print_user_info` that uses the `aiohttp` library to fetch user information from a random user API and prints out the user's name, email, and phone number.
Technology Stack : aiohttp, asyncio
Code Type : Asynchronous HTTP request
Code Difficulty : Intermediate
import aiohttp
import asyncio
import random
async def fetch_random_user(session):
url = "https://randomuser.me/api/"
async with session.get(url) as response:
return await response.json()
async def print_user_info():
async with aiohttp.ClientSession() as session:
user_info = await fetch_random_user(session)
print(f"Name: {user_info['results'][0]['name']['first']} {user_info['results'][0]['name']['last']}")
print(f"Email: {user_info['results'][0]['email']}")
print(f"Phone: {user_info['results'][0]['phone']}")