Random User Info API Endpoint

  • Share this:

Code introduction


This function defines a REST API endpoint that returns a random user information.


Technology Stack : Falcon, JSON, Random

Code Type : REST API Endpoint

Code Difficulty : Intermediate


                
                    
import falcon
import json
import random

def random_user_info(request, response):
    # Generate a random user info in JSON format
    users = [
        {"name": "Alice", "age": 25, "city": "New York"},
        {"name": "Bob", "age": 30, "city": "Los Angeles"},
        {"name": "Charlie", "age": 35, "city": "Chicago"}
    ]
    
    # Select a random user from the list
    selected_user = random.choice(users)
    
    # Convert the selected user to JSON format
    response.body = json.dumps(selected_user)

# JSON response format