Falcon Web API for Random Integer Generation

  • Share this:

Code introduction


This function creates a Falcon-based Web API that returns a random integer between 1 and 100.


Technology Stack : Falcon, json, random

Code Type : Web API

Code Difficulty : Intermediate


                
                    
import falcon
import json
import random

class RandomResource:
    def on_get(self, req, resp):
        # Generate a random integer between 1 and 100
        random_number = random.randint(1, 100)
        # Convert the random integer to a JSON response
        resp.body = json.dumps({"random_number": random_number})
        resp.content_type = 'application/json'

def random_resource_handler():
    return RandomResource()