Files
python-doc/Docs/Services/FastAPI/04-Post-Method.md
2026-01-19 21:42:45 +03:30

2.2 KiB
Raw Blame History

FastAPI POST Endpoint and JSON Input

This section demonstrates how to handle POST requests in FastAPI to create new resources using request data and return JSON responses.


Example Application (POST Request)

Extend main.py with the following code:

from fastapi import FastAPI

app = FastAPI()

users = [
    {"name": "abbas", "age": 20},
    {"name": "mmd", "age": 37},
    {"name": "asghar", "age": 19},
]


@app.get("/")
def home_page():
    return {"msg": "API is working"}


@app.post("/new_user")
def create_user(name: str, age: int):
    new_user = {"name": name, "age": age}
    users.append(new_user)
    return {"msg": "User created successfully"}

Code Overview

POST Endpoint Definition

@app.post("/new_user")
def create_user(name: str, age: int):
  • Registers an HTTP POST endpoint at /new_user

  • Accepts input parameters:

    • name → string
    • age → integer
  • FastAPI automatically validates input types


Creating a New Resource

new_user = {"name": name, "age": age}
users.append(new_user)
  • Constructs a new user object
  • Appends it to the in-memory users list
  • Simulates creating a record in a database

JSON Response

return {"msg": "User created successfully"}
  • Returns a structured JSON response
  • Automatically serialized by FastAPI

Example Request

Using curl

curl -X POST "http://localhost:8000/new_user?name=ali&age=25"

Response

{
  "msg": "User created successfully"
}

Verifying the Result

After creating a user, retrieve the updated list:

GET /users

Response will now include the newly added user.


Running the Application

Start the FastAPI service using uvicorn:

uvicorn main:app --reload

Best Practices

  • POST requests should be used to create resources
  • Avoid modifying in-memory data in production environments
  • Use request bodies with Pydantic models instead of query parameters for real APIs
  • Return appropriate HTTP status codes (201 Created)
  • Validate and sanitize all client-provided input
  • Replace in-memory storage with persistent databases