Skip to main content
Before you write a single line of FastAPI code, it pays to understand the mechanics that govern every conversation between a browser, a mobile app, or any API client and the server sitting on the other side. Every time you load a webpage, log in to an app, or fetch data, your device is participating in the Client-Server Model over the HTTP protocol β€” and once you see how it works, building APIs becomes far more intuitive.

The Client-Server Model

The web runs on a simple pattern of exchange. There are two roles β€” a client that asks for something and a server that responds:
  • Client (The Requester): Typically a web browser, mobile app, or command-line tool like curl. The client initiates communication by sending a Request.
  • Server (The Responder): A computer running software β€” like your FastAPI application β€” that listens for incoming requests, processes them (often querying databases), and sends back a Response.
Every API interaction you build in this course follows this exact cycle. Understanding it deeply will make every FastAPI concept you learn make immediate sense.

Anatomy of an HTTP Request

An HTTP request is a structured text block sent by the client containing four main components:
  1. HTTP Method (Verb): Tells the server what action to perform (e.g., GET, POST).
  2. URL / Path: The specific address of the resource (e.g., /api/v1/employees).
  3. Headers: Key-value pairs containing metadata (e.g., Content-Type: application/json, auth tokens).
  4. Body (Payload): The actual data being sent (e.g., the details of a new employee). GET and DELETE requests typically do not have bodies.
Here is a real example of an HTTP request:

URL Breakdown

Every URL contains several components. For the following address:

Full Request Component Reference


Anatomy of an HTTP Response

Once the server processes the request, it returns an HTTP response containing:
  1. Status Code: A three-digit number indicating the outcome (e.g., 200 OK, 404 Not Found).
  2. Headers: Metadata about the response (e.g., Content-Type: application/json).
  3. Body: The requested data, usually formatted as JSON for modern APIs.
Example response from a weather API:

HTTP Methods (Verbs)

HTTP methods define the semantic action the client wants to perform on a resource. In RESTful API design, you map these methods to CRUD (Create, Read, Update, Delete) operations:

API Endpoints and Path Parameters

An endpoint is a specific URL where your API provides access to a resource. Here’s how a standard Employee API looks:

Short Answer

You cannot tell by looking at the URL alone. It depends entirely on how the route is defined in the application.

Example

Consider this URL:
Is 101 part of the endpoint, or a path parameter? You can only know from the route definition.Case 1: 101 is a path parameter
Case 2: 101 is part of the fixed endpoint
Here 101 is a hard-coded part of the route β€” there is no path parameter at all.

Key Takeaway

You must always compare the URL with the route definition to determine which parts are parameters.

HTTP Status Codes

Status codes are grouped by their first digit, letting the client know immediately what kind of outcome occurred.

🟒 2xx: Success

  • 200 OK β€” The request succeeded and the server returned the requested data.
  • 201 Created β€” A new resource was successfully created.
  • 204 No Content β€” Success, but no body is returned (common for DELETE).

🟑 3xx: Redirection

  • 301 Moved Permanently / 307 Temporary Redirect β€” The resource is at a different location.

πŸ”΄ 4xx: Client Errors

  • 400 Bad Request β€” The server could not understand the request (e.g., invalid JSON).
  • 401 Unauthorized β€” Authentication is required (e.g., missing or invalid JWT).
  • 403 Forbidden β€” Authenticated but lacking permission.
  • 404 Not Found β€” The requested resource does not exist.
  • 422 Unprocessable Entity β€” Structure is correct but validation failed (e.g., a Pydantic error).

πŸ’₯ 5xx: Server Errors

  • 500 Internal Server Error β€” Something crashed on the server.
  • 503 Service Unavailable β€” The server is overloaded or under maintenance.
A 401 Unauthorized means the client isn’t authenticated at all. A 403 Forbidden means they are authenticated but don’t have permission. These are different errors and should be used precisely.

REST API Design Conventions

Clean, consistent URLs make your API intuitive to use. Follow these rules when designing your endpoints. Use nouns, not verbs. Use plural names. Keep paths lowercase.
Use path parameters to identify a specific resource:
Use query parameters for filtering, sorting, or pagination:
Rule of thumb: If the value uniquely identifies a resource, use a path parameter. If it filters or modifies how data is retrieved, use a query parameter.