Understanding HTTP: The Foundation of REST APIs
HTTP protocol, HTTP request, HTTP response, headers, body, URL structure, query parameters, path parameters
Understanding HTTP: The Foundation of REST APIs
REST APIs are built on top of HTTP (HyperText Transfer Protocol), the same protocol your browser uses to load web pages. Before you can design or build a REST API, you need to understand what an HTTP request and response look like and what information they carry.
The Structure of an HTTP Request
Every HTTP request has three parts: a request line, headers, and an optional body. The request line contains the HTTP method (GET, POST, etc.), the path (/users/42), and the HTTP version (HTTP/1.1). Headers are key-value pairs that carry metadata about the request — things like the content type, authorization tokens, and the client's accepted response formats. The body carries data sent to the server, typically in POST, PUT, and PATCH requests.
POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json
Authorization: Bearer eyJhbGci...
{
"name": "Bob Smith",
"email": "bob@example.com"
}The Structure of an HTTP Response
An HTTP response also has three parts: a status line, headers, and a body. The status line contains the HTTP version and a status code (200, 404, 500, etc.) along with a reason phrase. Response headers describe the content type, cache behavior, and security policies. The body contains the data returned to the client — usually a JSON object or array.
HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/users/99
{
"id": 99,
"name": "Bob Smith",
"email": "bob@example.com"
}URL Structure and Resource Paths
In REST APIs, URLs represent resources — the nouns of your API. A well-designed REST API URL clearly communicates what resource is being accessed. URLs follow a hierarchy: https://api.example.com/v1/users/42/orders. Breaking this down: https://api.example.com is the base URL, /v1 is the API version, /users is the resource collection, /42 is the specific user ID (a path parameter), and /orders is a nested resource belonging to that user.
Path Parameters vs Query Parameters
Path parameters identify a specific resource: /users/42 retrieves user with ID 42. Query parameters filter, sort, or paginate a collection and are appended after a question mark: /users?role=admin&page=2&limit=20. Use path parameters for identification and query parameters for modification of the result set.
Common HTTP Headers
Content-Type: application/json tells the server the format of the request body. Accept: application/json tells the server what format the client expects in the response. Authorization: Bearer token carries authentication credentials. Cache-Control manages caching behavior. Understanding these headers is essential for building correct, secure APIs.
