HTTP (Hypertext Transfer Protocol) is an application-layer networking protocol operating on top of TCP/IP (or UDP in HTTP/3). It defines how messages are formatted, transmitted, and processed between web clients (browsers, mobile apps, API clients) and web servers.
Set-Cookie headers, local storage, or JSON Web Tokens (JWT).
1. Anatomy of an HTTP Packet / Message
Every HTTP communication consists of two plain-text or binary structures: the **Request Packet** sent by the client and the **Response Packet** returned by the server.
Raw HTTP Request Example
Host: api.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Content-Type: application/json
Content-Length: 54
Accept: application/json
{
"email": "user@domain.com",
"password": "Pass1234!"
}
Raw HTTP Response Example
Date: Tue, 06 Oct 2026 16:00:00 GMT
Server: nginx/1.24.0
Content-Type: application/json; charset=UTF-8
Set-Cookie: auth_token=token_abc123; Secure; HttpOnly
Cache-Control: no-store
{
"status": "authenticated",
"user_id": 8042
}
2. Hands-on Code Examples in Real Programming Languages
A. cURL Command (Terminal / CLI)
curl -X POST https://api.example.com/v1/posts \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"title": "HTTP Full Guide", "category": "Networking"}'
B. JavaScript (Browser Fetch API)
async function createPost() {
try {
const response = await fetch('https://api.example.com/v1/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({ title: 'HTTP Full Guide', category: 'Networking' })
});
const data = await response.json();
console.log('Response:', data);
} catch (error) {
console.error('HTTP Error:', error);
}
}
C. Python (Requests Library)
url = "https://api.example.com/v1/posts"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
payload = {
"title": "HTTP Full Guide",
"category": "Networking"
}
response = requests.post(url, json=payload, headers=headers)
print("Status Code:", response.status_code)
print("Response JSON:", response.json())
3. Complete Reference of HTTP Request Methods
| Method | Description | Idempotent | Safe | Typical Use Case |
|---|---|---|---|---|
| GET | Retrieves data without modifying server state. | Yes | Yes | Loading blog pages, user profiles. |
| POST | Submits new payload data to create a resource. | No | No | Submitting forms, authenticating. |
| PUT | Completely replaces an existing target resource. | Yes | No | Updating entire profile records. |
| PATCH | Applies partial updates to a resource. | No | No | Changing only a user password or email. |
| DELETE | Removes the specified target resource. | Yes | No | Deleting a blog comment or account. |
4. HTTP Status Code Categories
- 1xx Informational: Protocol handshake status (e.g.,
100 Continue,101 Switching Protocols). - 2xx Success: Operation completed successfully (e.g.,
200 OK,201 Created,204 No Content). - 3xx Redirection: Further action required (e.g.,
301 Moved Permanently,302 Found,304 Not Modified). - 4xx Client Error: Invalid request format or authentication issue (e.g.,
400 Bad Request,401 Unauthorized,403 Forbidden,404 Not Found,429 Too Many Requests). - 5xx Server Error: Server failed to process a valid request (e.g.,
500 Internal Server Error,502 Bad Gateway,503 Service Unavailable,504 Gateway Timeout).
5. Security: HTTP vs HTTPS Protocol Handshake
Standard HTTP transmits data in clear plain text, making it susceptible to Packet Sniffing and Man-In-The-Middle (MITM) attacks. **HTTPS (HTTP Secure)** encapsulates HTTP inside a **TLS/SSL Encryption Tunnel** over Port 443.
| Feature | HTTP | HTTPS |
|---|---|---|
| Encryption | None (Plaintext) | TLS 1.2 / TLS 1.3 Encryption |
| Default Network Port | Port 80 | Port 443 |
| Integrity Verification | No protection against tampering | Cryptographic MAC prevents data alteration |
| Domain Identity | Unverified | Verified via SSL/TLS Certificates (CA) |
No comments:
Post a Comment