Asynchronous Random User Fetch with httpx Library

  • Share this:

Code introduction


This function uses the httpx library to asynchronously fetch random user information from the JSONPlaceholder API.


Technology Stack : httpx

Code Type : Asynchronous HTTP request

Code Difficulty : Intermediate


                
                    
import httpx
import random

def fetch_random_user_info():
    url = "https://jsonplaceholder.typicode.com/users"
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        user = response.json()[random.randint(0, 9)]
        return user

# JSON output                
              
Tags: