HTTP WEB NETWORKING HEADERS COOKIES CACHING

How HTTP Works: Headers, Status Codes, Caching, and Cookies

⏱️ 3 min read
👨‍💻

How HTTP Works: Headers, Status Codes, Caching, and Cookies

1. Introduction: Why HTTP Still Matters

All modern web apps—REST, GraphQL, even WebSockets—are built on top of HTTP.

HTTP is more than a request/response protocol—it’s a key component of performance, security, and debugging.

Analogy: “HTTP is the diplomatic language between browsers and servers.”

2. HTTP Request and Response: The Basic Anatomy

a. Example HTTP Request Structure

GET /index.html HTTP/1.1
Host: blog.hasanh.dev
User-Agent: Mozilla/5.0
Accept: text/html

b. Example HTTP Response Structure

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1024
Set-Cookie: session_id=abc123; HttpOnly

Key Components:

3. Headers: The Brain of the HTTP Protocol

Headers define how HTTP requests and responses are handled.

Types of Headers:

Analogy: “Headers are the envelope that describes the contents of a letter.”

Important Headers:

4. Status Codes: The Server’s Sign Language

Categories:

Quick Debugging Tips:

5. HTTP Caching: Speed Up, Lighten the Load

Caching improves speed and reduces server load.

Key Cache-Control Directives:

Effective Combos:

Cache Strategy:

Use DevTools > Network to inspect cache headers.

6. Cookies: Identity & Session in the Web

Cookies are small key-value pairs sent by the browser to the server.

Common uses:

Key Attributes:

Comparison Table:

FeatureCookieslocalStoragesessionStorage
Sent to server✅ Yes❌ No❌ No
Max size~4KB~5-10MB~5-10MB
Persist on reload✅ Yes✅ Yes❌ No

7. Full Flow Example: Login Using Cookies

  1. User sends POST /login
  2. Server validates and responds with Set-Cookie
  3. Browser automatically stores the cookie
  4. Future requests auto-include Cookie header
  5. Server authenticates using cookie data

8. Common Mistakes & Best Practices

9. Conclusion

HTTP is more than just a bridge—it’s the foundation of web application architecture.

Mastering HTTP means:

Practice it in real projects: open DevTools, try curl, or use Postman/Insomnia!