Back GET | POST | PUT | DELETE - Fast API 23 Jun, 2026

In FastAPI, HTTP methods define what action the client wants to perform on a resource.

Think of a simple User Management API:

MethodPurposeExample
GETRead dataGet user details
POSTCreate new dataCreate a new user
PUTUpdate existing dataUpdate user information
DELETERemove dataDelete a user

1. GET Request

Used to retrieve data from the server.

FastAPI Example

from fastapi import FastAPI

app = FastAPI()

@app.get("/users/{user_id}")
def get_user(user_id: int):
    return {"user_id": user_id, "name": "Abhishek"}

Request

GET /users/1

Response

{
    "user_id": 1,
    "name": "Abhishek"
}

Use Cases

✅ Fetch User
✅ Fetch Products
✅ Fetch Reports


2. POST Request

Used to create new data.

FastAPI Example

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    name: str
    age: int

@app.post("/users")
def create_user(user: User):
    return {
        "message": "User created",
        "data": user
    }

Request Body

{
    "name": "Abhishek",
    "age": 35
}

Response

{
    "message": "User created",
    "data": {
        "name": "Abhishek",
        "age": 35
    }
}

Use Cases

✅ Register User
✅ Upload File
✅ Create Order


3. PUT Request

Used to update an existing resource completely.

FastAPI Example

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    name: str
    age: int

@app.put("/users/{user_id}")
def update_user(user_id: int, user: User):
    return {
        "message": "User updated",
        "user_id": user_id,
        "data": user
    }

Request

PUT /users/1

Body:

{
    "name": "Abhishek Agnihotri",
    "age": 36
}

Response

{
    "message": "User updated",
    "user_id": 1,
    "data": {
        "name": "Abhishek Agnihotri",
        "age": 36
    }
}

Use Cases

✅ Update Profile
✅ Update Product Details
✅ Update Employee Record


GET vs POST vs PUT

FeatureGETPOSTPUT
PurposeRead DataCreate DataUpdate Data
Sends BodyUsually NoYesYes
Changes Server DataNoYesYes
Idempotent*YesNoYes
ExampleGet UserCreate UserUpdate User

What is Idempotent?

An operation is idempotent if calling it multiple times gives the same result.

GET

GET /users/1
GET /users/1
GET /users/1

Same data returned every time.

PUT

PUT /users/1
{
    "name":"John"
}

Running it 10 times still leaves the user as:

{
    "name":"John"
}

POST

POST /users
{
    "name":"John"
}

Running it 10 times creates 10 users.


Typical FastAPI CRUD Example

@app.post("/users")
def create_user():
    pass

@app.get("/users")
def get_users():
    pass

@app.get("/users/{id}")
def get_user(id: int):
    pass

@app.put("/users/{id}")
def update_user(id: int):
    pass

@app.delete("/users/{id}")
def delete_user(id: int):
    pass

This is the standard REST API pattern used in FastAPI applications.

Real-world Example: Research Report Application

@app.get("/reports")

➡ List all reports

@app.get("/reports/10")

➡ View report #10

@app.post("/reports")

➡ Generate/Create a new report

@app.put("/reports/10")

➡ Update report #10

@app.delete("/reports/10")

➡ Remove report #10

This pattern is what you'll use most often when building FastAPI applications with routers (APIRouter), SQLAlchemy, and CRUD operations.

Rate This Note
Login to Rate This Note