Asynchronous Random User Fetch Using httpx Library

  • Share this:

Code introduction


This function uses the httpx library to send an asynchronous HTTP GET request to the randomuser.me API and parses the random user data from the response.


Technology Stack : httpx, async, await, json

Code Type : Asynchronous HTTP request function

Code Difficulty : Intermediate


                
                    
def fetch_random_user():
    import httpx
    import random
    import json

    # URL of the API that provides random user data
    url = "https://randomuser.me/api/"

    # Send a GET request to the API
    async with httpx.AsyncClient() as client:
        response = await client.get(url)

    # Parse the JSON response
    data = response.json()

    # Extract the random user data
    user = data['results'][0]

    # Return the user data as a dictionary
    return user