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:- HTTP Method (Verb): Tells the server what action to perform (e.g.,
GET,POST). - URL / Path: The specific address of the resource (e.g.,
/api/v1/employees). - Headers: Key-value pairs containing metadata (e.g.,
Content-Type: application/json, auth tokens). - Body (Payload): The actual data being sent (e.g., the details of a new employee).
GETandDELETErequests typically do not have bodies.
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:- Status Code: A three-digit number indicating the outcome (e.g.,
200 OK,404 Not Found). - Headers: Metadata about the response (e.g.,
Content-Type: application/json). - Body: The requested data, usually formatted as JSON for modern APIs.
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:How do I tell if a URL segment is a path parameter or part of the endpoint?
How do I tell if a URL segment is a path parameter or part of the endpoint?
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:101 part of the endpoint, or a path parameter? You can only know from the route definition.Case 1: 101 is a path parameterCase 2:
101 is part of the fixed endpoint101 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 forDELETE).
π‘ 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.