HTTP Get Request Enhancement

 Get Request Enhancement

Sample code as we are going to now print the list of Books in new route:

If you will type host:8000/docs then you will get default swagger looks for FastAPI UI which will also help you to test the result.

# This code is to create the api endpoint for providing list of books as result to user whenever a link is clicked.
from fastapi import FastAPI

app = FastAPI() # allow uvicorn to identify the application is created by using FastAPI and it is the entry point for the application.
# This is the first API endpoint that will be created to return a simple message.


BOOKS = [
    {'title': 'Title one', 'author': 'Author one', 'category': 'science'},
    {'title': 'Title two', 'author': 'Author two', 'category': 'science'},
    {'title': 'Title three', 'author': 'Author three', 'category': 'history'},
    {'title': 'Title four', 'author': 'Author four', 'category': 'math'},
    {'title': 'Title five', 'author': 'Author five', 'category': 'math'}
]
@app.get("/api-endpoint") # It is a decorator that tells FastAPI that this function is an API endpoint and will call the function below when a GET request is made to the specified path.
async def first_api():
    return {"message": "Hello Puneet!"}

@app.get("/books")
async def get_books():
    """
    This function returns the list of books.
    """
    return BOOKS



Comments

Popular posts from this blog

Post Request with Pydantic usage (input validator)

Code Scalability and Routing

CRUD Assignment