Introduction

FastAPI is a modern web framework that empowers developers to build web APIs quickly and efficiently using Python. A POST endpoint in FastAPI allows clients to submit data to the server, often for creating new resources. One part of a POST request ist the Request Body, which contains the data you want to send to the server. In this tutorial, we’ll explain how create a simple POST endpoint and how to define the request body in FastAPI.

Goal

💡
We want to build an API allowing clients to create new datasets in a sample dataset. Additionally, we want to specify the request body.

Prerequisites

☑️ Set up a virtual environment in the root of your project directory

Set up a Python Virtual Environment on Mac: A Step-by-Step Guide
Introduction In this tutorial, we show you step-by-step how to set up a Python virtual environment on your Mac. Why Use a Virtual Environment? A virtual environment allows you to create isolated environments for different Python projects. This helps to avoid conflicts between project dependencies and ensures that each project

☑️ Set up a FastAPI project

How to set up a FastAPI Project
Introduction FastAPI has quickly gained popularity as a modern, fast and easy-to-use Python web framework for building RESTful APIs. In this tutorial, we show you step-by-step how to set up a FastAPI project. Prerequisites First of all, make sure you have Python installed on your system. Furthermore, it is recommended

What is a POST Endpoint?

POST endpoint is a specific URL in an API where clients send data to the server to create a new resource.

What is a Request Body?

In a POST request the request body is the part of the request that contains the data you want to send to the server. For example, when you submit a form, that data is included in the request body. The server then processes this data to perform actions like creating a user, updating records, or uploading files.

Step 1: Import Libraries

First, we import the following python modules:

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

Step 2: Define Sample Data

Imagine a system where you can find information about various online courses. Let's define a sample database:

sample_courses_db = [
    {"course_id": "c1", "course_name": "Data Science"}, 
    {"course_id": "c2", "course_name": "Data Engineering"}, 
    {"course_id": "c3", "course_name": "Data Analytics"}
    ]

Step 3: Create FastAPI Instance

Let's create a FastAPI instance:

app = FastAPI()

Step 4: Define Request Body

Let's define a request body for the POST endpoint. The request body should contain the course_id and the  course_name. In order to define a request body we create the following Pydantic model:


class Course(BaseModel):
    course_id: str
    course_name: str | None = None

Step 5: Create POST Endpoint

Now, we want to create a POST endpoint that should meet the following requirements:

  • If the course_id doesn't exist add it to the sample database.
  • If the course_id already exists in the database an error message should be raised.

Let's set up a POST endpoint:

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In