Fetching Random User Info from Public API

  • Share this:

Code introduction


This function fetches random user information from a public API at https://randomuser.me/api/. It uses the asynchronous features of the httpx library to send an HTTP request and extract user information from the returned JSON data.


Technology Stack : httpx, Python asynchronous programming

Code Type : Asynchronous HTTP request

Code Difficulty : Intermediate


                
                    
import httpx
import random

def fetch_random_user_info():
    """
    Fetches a random user info from a public API.
    """
    url = "https://randomuser.me/api/"
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        data = response.json()
        return data['results'][0]

# JSON Explanation