EXERCISE
1TCP delivers data. HTTP defines what that data means. Learn the protocol powering every website and API.
Save
HTTP (HyperText Transfer Protocol): The common language of the web.
Purpose: Defines how to format requests and responses.
Runs on TCP: HTTP uses TCP for reliable delivery.
Most APIs you build: using HTTP.
Structure:
GET /api/users/123 HTTP/1.1
Host: api.example.com
User-Agent: Mozilla/5.0
Accept: application/json
(optional body)
Parts:
Request line: Method (GET), path (//users/123), version (HTTP/1.1)
Headers: Key-value metadata (Host, User-Agent, Accept)
Body (optional): Data sent to (, form data)
GET: Retrieve data. No body.
POST: Create resource. Has body.
PUT: Update entire resource. Has body.
DELETE: Remove resource. Usually no body.
PATCH: Partial update. Has body.
Example GET:
GET /api/products/456 HTTP/1.1
Host: store.com
Server returns product with ID 456.
Example POST:
POST /api/products HTTP/1.1
Host: store.com
Content-Type: application/json
{"name": "Laptop", "price": 999}
Server creates product, returns it with new ID.
Structure:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 87
{"id": 123, "name": "John", "email": "john@example.com"}
Parts:
Status line: Version, status code (200), message (OK)
Headers: Content-Type, Content-Length, etc.
Body: Actual data (JSON, HTML, image bytes)
2xx Success:
4xx Client Errors:
5xx Server Errors:
HTTP/1.1 default behavior: Connection closes after response.
Flow:
For next request: Repeat entire flow!
Why? HTTP designed for simple document retrieval. One request per page.
Modern reality: Page load needs 50-100 requests (HTML, CSS, JS, images, API calls).
Problem: New TCP connection per request is extremely expensive.
Tell server to keep connection open.
Request:
GET /api/data HTTP/1.1
Host: api.example.com
Connection: keep-alive
Response:
HTTP/1.1 200 OK
Connection: keep-alive
(data)
Connection stays open. Next request reuses it. No new handshake.
Modern browsers: Automatically include keep-alive.
Modern servers (, Apache): Support it by default.
Backend services maintain pool of persistent connections.
Example: app → .
Without pooling: New TCP connection per query. Slow.
With pooling: Maintain 10 persistent connections. Reuse them. Fast.
Universal: Every language has HTTP libraries.
Human readable: Debug with curl, Postman, browser tools.
Stateless: Each request independent. Easy to scale.
Standardized: Everyone understands GET, POST, status codes.
HTTP is the lingua franca of the internet.