You can download this code by clicking the button below.
This code is now available for download.
This function uses the Starlette framework to create a simple web API. It accepts an HTTP GET request and retrieves the user ID from the query parameters. If the user ID exists, it returns a JSON response containing the user ID; otherwise, it returns an error message.
Technology Stack : Starlette
Code Type : Web API Function
Code Difficulty : Intermediate
def random_starlette_function():
from starlette.responses import JSONResponse
from starlette.requests import Request
from starlette.status import HTTP_200_OK
def get_user_data(request: Request):
user_id = request.query_params.get('user_id')
if user_id:
return JSONResponse(content={"user_id": user_id}, status_code=HTTP_200_OK)
else:
return JSONResponse(content={"error": "User ID is required"}, status_code=HTTP_200_OK)
return get_user_data