Defines how clients (e.g., browsers) and servers communicate using a stateless request–response model. HTTP is an application-layer protocol that runs on top of TCP.
HTTP Communication Model
HTTP follows a simple cycle:
Client (Browser)
│
├── HTTP Request
│
▼
Server
│
├── HTTP Response
│
▼
Client receives data (HTML / JSON / etc.)
HTTP Request
A request is sent from the client to the server.
Structure:
- Request Line
- Method (
GET,POST,PUT,DELETE) - URL path
- HTTP version
- Method (
- Headers
- Metadata (
auth,content type,cookies, etc.)
- Metadata (
- Body (optional)
- Data sent to server (e.g.,
JSONpayload)
- Data sent to server (e.g.,
Example:
GET /api/users HTTP/1.1
Host: example.com
Authorization: Bearer <token>
HTTP Response
A response is sent from the server back to the client.
Structure:
- Status Line
- HTTP version
- Status code
- Headers
- Metadata (
Content-Type,caching, etc.)
- Metadata (
- Body
- Actual response data (
HTML,JSON, etc.)
- Actual response data (
Example:
HTTP/1.1 200 OK
Content-Type: application/json
{
"name": "Alice"
}
HTTP Methods
Defines the type of operation the client wants to perform.
| Method | Description | Typical Behavior | Side Effects | Idempotent |
|---|---|---|---|---|
| GET | Retrieve data from the server without modifying it | Fetch a resource (e.g., webpage, API data) | No | Yes |
| POST | Send data to the server to create a new resource or trigger processing | Submit form data, create records | Yes | No |
| PUT | Replace an existing resource or create it if it does not exist | Full update of a resource | Yes | Yes |
| DELETE | Remove a resource from the server | Delete a file, database entry, or object | Yes | Yes |
Side effects: the operation changes something on the server or system state. Idempotent: doing the same operation multiple times has the same final effect as doing it once.
REST API
REST API is an architectural style for designing the application programming interface (API), commonly used over HTTP, to follow standard HTTP methods.
Key principles
- Resource-based
- Everything is a resource (users, orders, files)
- Identified by URLs (e.g.,
/users/1)
- Stateless
- Each request contains all necessary information
- Server does not store client session state
- Uses HTTP methods
GET: read resourcePOST: create resourcePUT: update resourceDELETE: remove resource
Public REST API
Some web servers or managed backend services with exposed public REST APIs, allowing users or applications to directly access and manipulate resources over HTTP. This is common in cloud services, backend platforms, and database.
HTTP Headers
Contain metadata about the request or response, providing additional context on how the message should be interpreted, processed, and handled.
Common header categories:
-
Authentication (
Authorization)- Carries credentials used to authenticate the client
- Common formats include:
Bearer <JWT token>- API keys
- Used to verify identity and permissions before processing the request
-
Data format (
Content-Type)- Specifies the format of the request or response body
- Examples:
application/jsontext/htmlmultipart/form-data
- Ensures both client and server correctly parse the payload
-
Cookies (
Cookie,Set-Cookie)- Used to maintain session state between requests
- Client sends stored cookies using
Cookieheader - Server can set or update cookies using
Set-Cookie - Commonly used for:
- session management
- user tracking
- login persistence
-
Caching rules (
Cache-Control,ETag)- Controls how responses are cached by browsers or intermediate proxies
- Examples:
Cache-Control: no-cacheCache-Control: max-age=3600
ETaghelps determine whether cached content is still valid- Improves performance and reduces unnecessary network requests
Body
Contains actual data payload:
- JSON
- HTML
- Form data (for
POST) - File uploads
HTTP Status Codes
Indicate result of a request. The commonly used status codes are:
| Code | Meaning | Description |
|---|---|---|
| 200 | OK | Request succeeded |
| 201 | Created | Resource created successfully |
| 400 | Bad Request | Invalid request format |
| 401 | Unauthorized | Authentication required |
| 403 | Forbidden | No permission |
| 404 | Not Found | Resource does not exist |
| 500 | Internal Server Error | Server-side failure |
| 502 | Bad Gateway | Invalid response from upstream server |
curl (Command-line HTTP Client)
curl is a command-line tool used to send HTTP requests and inspect HTTP responses directly from the terminal. It is commonly used for debugging, testing APIs, and monitoring network communication.
Basic usage
Send a GET request
curl https://example.com
Send a POST request
curl -X POST https://example.com/api/users \
-H "Content-Type: application/json" \
-d '{"name":"Alice"}'
Alternatively, one can use Postman for graphical interface.