Get startedGet started for free

GET and POST requests for AI

1. GET and POST requests for AI

Welcome to this course on using FastAPI with AI models.

2. Our instructor

Our instructor is Matt Eckerle. He is a software and data engineering leader and currently Enterprise Data Manager at Inari. He has been using FastAPI for machine learning applications since 2019.

3. Course overview

Throughout the course we'll learn how to use FastAPI with AI by creating APIs for model predictions. We'll explore how to handle various requests, integrate models, and validate inputs, as well as understand security, optimization, versioning, and monitoring. By the end, we will be equipped to create and maintain production-grade APIs for AI models.

4. Before we start

Before starting, we should have basic knowledge of Python, HTTP, and REST API concepts. We should be familiar with using the FastAPI framework, including handling GET and POST requests and using Pydantic models, and have a foundational understanding of machine learning.

5. GET requests explained

Let's review a few concepts first. To understand GET requests, think of going to a restaurant and asking to see the menu. GET requests are built to retrieve data. In APIs, we often include extra information within the URL itself, like an item ID. This is called a path parameter. A GET request doesn't change anything in the database - it is read-only.

6. Implementing GET with path parameters

Here's how we implement a GET endpoint in FastAPI. We use the @app.get decorator to create a GET endpoint. Decorators can modify how a function behaves, add extra steps before the function runs, or even replace the function entirely. Here the decorator is telling the framework that the async function read_item should handle GET requests to the /item endpoint. The "{item_id}" in the path is our path parameter. By adding the ": int" type hint, FastAPI automatically validates that it's a number.

7. POST requests explained

Now, let's look at POST requests. If GET requests are like asking to see the menu at a restaurant, POST requests are like placing an order. POST requests send a data payload to the server, usually to create or update something. This data typically comes in the form of a JSON request body. POST requests can update data and change server state.

8. Implementing POST with JSON data

To implement POST request handling in FastAPI, we have a sample database dictionary called db. We define a Pydantic model to structure our data. The @app.post decorator sets up our POST endpoint along with the 201 status_code for successful requests. This status code indicates the successful creation of an object. FastAPI then automatically parses the incoming JSON and validates it against our model. We save the item dictionary in our database against the item name.

9. HTTP status codes

Common HTTP status codes include 200 for OK, 404 for not found, and 201 for a successful POST request that creates a new object in a database.

10. Raising HTTP exceptions

In a FastAPI app, raising an HTTP exception means triggering a specific HTTP error response, usually to indicate a problem or specific condition to the client. Let's put both GET and POST endpoints together. Here, we use HTTPException to return a 404 status when an item isn't found in a GET request, and we return a 201 status when we successfully create an item as part of the POST request.

11. Let's practice!

Time to practice!