Understanding HTTP and Web communication
What is HTTP?
Last updated: 2/28/2025
1 hour
Medium
Understanding HTTP and Web Communication
1. What is HTTP?
HTTP (HyperText Transfer Protocol) is the foundation of communication on the web. It is a request-response protocol used by clients (like browsers, mobile apps, and APIs) to communicate with servers.
How HTTP Works:
- A client sends a request to a server.
- The server processes the request and sends a response back.
- The response includes a status code, headers, and optional data (like JSON).
2. HTTP Request Structure
An HTTP request consists of:
- Method (GET, POST, etc.)
- URL (The endpoint being requested)
- Headers (Metadata about the request)
- Body (Optional, used for sending data)
Example HTTP Request:
GET /books HTTP/1.1 Host: example.com Accept: application/json
Example HTTP Request with Data (POST):
POST /books HTTP/1.1 Host: example.com Content-Type: application/json { "title": "Clean Code", "author": "Robert C. Martin" }
3. HTTP Methods
HTTP defines different methods for interacting with resources:
Method | Description |
---|---|
GET | Retrieve data (Read) |
POST | Create a new resource |
PUT | Update/replace a resource |
PATCH | Partially update a resource |
DELETE | Remove a resource |
Example API Endpoints:
Action | HTTP Method | Endpoint |
---|---|---|
Get all books | GET | /books |
Get a specific book | GET | /books/:id |
Create a new book | POST | /books |
Update a book | PUT | /books/:id |
Delete a book | DELETE | /books/:id |
4. HTTP Response Structure
A server's response includes:
- Status Code (Indicates success or error)
- Headers (Metadata)
- Body (Contains requested data)
Example Response:
HTTP/1.1 200 OK Content-Type: application/json [ { "id": 1, "title": "Clean Code", "author": "Robert C. Martin" } ]
5. HTTP Status Codes
Status codes indicate the result of a request.
Common Status Codes:
Code | Meaning |
---|---|
200 OK | Success |
201 Created | Resource created |
400 Bad Request | Invalid request from client |
401 Unauthorized | Authentication required |
403 Forbidden | Access denied |
404 Not Found | Resource does not exist |
500 Internal Server Error | Server failure |
6. Headers in HTTP Requests and Responses
Headers provide metadata about a request or response.
Common Headers:
Header | Description |
---|---|
Content-Type | Specifies the format of the body (e.g., application/json ) |
Authorization | Used for authentication (e.g., API keys, JWT) |
Accept | Specifies the response format the client expects |
Cache-Control | Controls caching behavior |
Example of Headers in an API Request:
GET /books HTTP/1.1 Host: example.com Accept: application/json Authorization: Bearer <token>
7. Testing HTTP Requests
You can test HTTP requests using:
- Postman (GUI tool)
- cURL (Command-line tool)
- Your browser (For simple
GET
requests)
Example cURL Request:
curl -X GET "https://example.com/books" -H "Accept: application/json"
8. Summary
- HTTP is the foundation of web communication.
- Clients make requests and servers send responses.
- Different HTTP methods (GET, POST, PUT, DELETE) define how we interact with APIs.
- Status codes indicate the result of the request.
- Headers help manage data format, authentication, and caching.