HTTP - The Backbone of API Communication
Hypertext Transfer Protocol is used to transfer Hypertext (outdated term for web related text) and is the most common protocol for web and API communication. HTTP is:
- Stateless: Each request is independent; the server doesn’t remember what you did before.
- Request-response based: The client sends a request, the server sends back a response.
HTTP Request Flow (How It Works)
-
Your browser or app opens a TCP connection to the server.
-
It sends an HTTP request:
GET /user/42 HTTP/1.1 Host: api.example.com
-
The server responds:
200 OK Content-Type: application/json { "id": 42, "name": "Alice" }
-
Connection is either closed or reused, depending on HTTP version.
Common HTTP Methods
Method | Use |
---|---|
GET | Fetch data (e.g., get user profile) |
POST | Create a resource (e.g., add new user) |
PUT | Replace a resource (e.g., update user info) |
PATCH | Modify part of a resource |
DELETE | Remove a resource |
OPTIONS | Discover supported methods for a resource |
TRACE | Diagnostic method for debugging |
CONNECT | Establish a tunnel (e.g., HTTPS) |
HTTP Headers
HTTP headers are name-value pairs that carry metadata with requests and responses.
Examples:
Content-Type: application/json
- tells the server what kind of data you’re sendingAuthorization: Bearer <token>
- used for authenticationCookie: session_id=xyz
- Maintains session infoUser-Agent: Chrome/120.0
- info about the client
Stateless but Extendable
Although HTTP is stateless, meaning the server doesn’t remember previous requests, we use tools like Cookies, Sessions, Tokens (JWTs) to maintain context across multiple interactions.
HTTP Versions in Brief
Version | Key Characteristics |
---|---|
HTTP/0.9 | Single-line protocol with only GET method and HTML-only responses |
HTTP/1.0 |
|
HTTP/1.1 |
|
HTTP/2.0 |
|
HTTP/3.0 |
|
Note
- Most HTTP connection should use HTTPS over HTTP/2 and if that doesn’t work should use HTTP 1.1