You can download this code by clicking the button below.
This code is now available for download.
This function accepts an HTTP request, generates a random number between 1 and 100, and returns this number in JSON format.
Technology Stack : Starlette, random
Code Type : API Function
Code Difficulty : Intermediate
from starlette.responses import JSONResponse
from starlette.requests import Request
from starlette.routing import Route
def random_number_generator(request: Request):
# Generate a random number between 1 and 100
import random
random_number = random.randint(1, 100)
# Create a JSON response with the random number
response = JSONResponse({"random_number": random_number})
return response
# JSON representation of the code