HTTP & HTTPS
Basics
Visual Representation
✉️ The Postcard vs Sealed Envelope Analogy
Think of it like sending a message through the postal system.
HTTP (HyperText Transfer Protocol) is like sending a postcard. Your message is written in plain text — the postman, the sorting office, anyone who handles it along the way can read what's on it. "Dear Bank, my password is 1234." Yikes.
HTTPS (HTTP Secure) is like putting that message inside a sealed, locked envelope. The same letter travels the same route, but nobody in between can open it. Only the intended recipient has the key.
HTTP is the LANGUAGE that your browser and servers use to talk to each other. HTTPS is the same language, but whispered through a secret encrypted tunnel so nobody can eavesdrop.
💡 Simple Summary: HTTP = rules for how browsers and servers communicate. HTTPS = same rules but with encryption (a locked envelope) so no one can spy on the conversation.
🌐 What Happens When You Visit a Website (Step by Step)
Let's say you type "www.swiggy.com" in your browser:
Step 1: Your browser builds a message (called a "request"). It's like writing a formal letter: "Dear Swiggy, please send me your homepage."
Step 2: If it's HTTPS (which it is — look for the 🔒 lock icon), your browser first does a secret handshake with Swiggy's server. They agree on a secret code that only they know. Now everything is encrypted.
Step 3: Your browser sends the request through this encrypted tunnel.
Step 4: Swiggy's server receives it, processes it, and sends back a "response" — the HTML, CSS, images, and data that make up the page you see.
Step 5: Your browser takes that response and paints the beautiful Swiggy homepage on your screen.
This whole thing happens in under a second. And it happens EVERY time you click a link, submit a form, or scroll to load more content.
📤 The Request: What Your Browser Sends
Every HTTP request has these parts (like a formal letter):
1. Method — What do you want to do?
2. URL — Where is it? Like "/restaurants/mumbai" or "/orders/12345"
3. Headers — Extra info, like metadata on an envelope:
4. Body (optional) — The actual data you're sending. Used with POST and PUT. Like the contents of the letter inside the envelope.
📥 The Response: What Comes Back
The server responds with:
1. Status Code — A number telling you what happened (more on this below!)
2. Headers — Metadata about the response (what format, how long to cache it, etc.)
3. Body — The actual data (HTML for a webpage, JSON for an API, image bytes for a photo).
💡 Simple Summary: Your browser sends a request (method + URL + headers + body). The server sends back a response (status code + headers + body). That's the entire conversation.
🍕 GET vs POST vs PUT vs DELETE — The Swiggy Example
Let's use Swiggy to understand HTTP methods. Think of them as different actions at a restaurant:
GET = Browse the menu 📋
"Show me all restaurants near me." You're just LOOKING. Nothing changes.
\GET /restaurants?city=mumbai\
POST = Place a new order 🛒
"I want to place THIS order." You're CREATING something new.
\POST /orders\ with body: { items: ["butter chicken", "naan"], address: "..." }
PUT = Update your order ✏️
"Change my address for order #456." You're MODIFYING something that exists.
\PUT /orders/456\ with body: { address: "new address" }
DELETE = Cancel your order ❌
"Cancel order #456." You're REMOVING something.
\DELETE /orders/456\
❓ But wait — can't you just use POST for everything?
Technically yes, but methods have MEANING that helps the entire system work better. GET means "safe to retry" — if your browser fails, it can try again without placing a duplicate order. POST means "something will change" — retrying might create two orders! Caches store GET responses but ignore POST. This matters.
💡 Simple Summary: GET = read, POST = create, PUT = update, DELETE = remove. These aren't just labels — they tell the entire internet infrastructure how to handle your request.
🔢 Status Codes: What They Mean (With Fun Examples)
Status codes are 3-digit numbers the server sends back. They tell your browser what happened:
2xx = SUCCESS 🎉 (everything worked!)
3xx = REDIRECT 🔀 (go somewhere else)
4xx = YOU messed up 😅 (client error)
5xx = SERVER messed up 💥 (server error)
Real fun examples:
💡 Simple Summary: 2xx = good news. 4xx = you did something wrong. 5xx = the server broke. Remember these numbers — interviewers love asking about them.
🔒 HTTPS: Why It Actually Matters
Imagine you're at a coffee shop using public WiFi. You open your banking app and type your password.
Without HTTPS (HTTP): Your password travels as plain text through the air. ANYONE on that same WiFi with a simple tool can see: "username: prakshay, password: mysecretpass123." They can steal your account.
With HTTPS: Your password is encrypted before it leaves your phone. Even if someone intercepts it, they see: "x7#kQ9$mL..." — complete gibberish without the decryption key.
That 🔒 lock icon in your browser means HTTPS is active. The connection is encrypted. No one between you and the server can read or modify the data.
How does HTTPS work? (The secret handshake, simplified):
❓ But wait — doesn't encryption slow things down?
It used to add 100-200ms. But modern HTTPS (TLS 1.3) does the handshake in just ONE round trip. Plus, HTTP/2 (which requires HTTPS) is actually FASTER than old HTTP because it sends multiple things at once. So HTTPS is now both more secure AND faster. There's zero reason to use plain HTTP in 2024.
💡 Simple Summary: HTTPS = your data is locked in a vault while traveling. Without it, anyone on your network can read your passwords, messages, and bank details.
🎯 Key Points for Interviews
❌ Common Mistakes in Interviews
Mistake 1: Not knowing status codes — saying "the server returns an error" without specifying 400 vs 500. 400 = client's fault. 500 = server's fault. Big difference!
Mistake 2: Saying HTTP is never used — HTTP is still used for internal service-to-service communication within private networks. TLS everywhere internally adds CPU overhead.
Mistake 3: Confusing 401 and 403 — 401 = "Who are you? Log in." 403 = "I know who you are, but you can't access this." Authentication vs Authorization.
Mistake 4: Forgetting idempotency — GET is idempotent (calling it 10 times = same result). POST is NOT (10 POSTs might create 10 orders). This matters for retries!
Mistake 5: Not mentioning HTTP/2 — If you're designing a modern system and don't mention multiplexing, you're missing points.
🎯 Interview One-Liner
"HTTP defines the request-response communication with methods and status codes, HTTPS adds TLS encryption to prevent eavesdropping, and HTTP/2's multiplexing over a single connection makes modern HTTPS actually faster than the old HTTP/1.1."
Interview Q&A
Q: Explain the TLS handshake in simple terms.
Client says "let's be secure" and lists encryption methods it knows. Server picks one and shows its digital certificate (issued by a trusted authority). Client verifies the cert is legit, then both sides generate a shared secret key using math (without ever sending the key itself over the network!). From then on, all data is encrypted with this key. TLS 1.3 does this in just one round-trip.
Q: Why do we have different HTTP methods? Can't everything just be POST?
Methods carry MEANING that infrastructure uses. GET means "safe to retry and cacheable." POST means "something changes, don't blindly retry." Caches store GET responses. Load balancers can safely retry failed GETs. If everything were POST, you might accidentally place 3 duplicate orders when the network retries.
Q: What's the difference between 401 and 403?
401 = "Who are you? Show me your ID." Fix: log in or provide a valid token. 403 = "I know who you are, but you're not on the VIP list." Fix: get proper permissions. Think bouncer at a club — 401 is "show your ID," 403 is "you're not on the guest list."
Q: How does HTTP/2 improve performance?
HTTP/1.1 sends one request per connection, and browsers open max 6 connections per domain. HTTP/2 sends UNLIMITED requests over ONE connection — all interleaved (multiplexing). It also compresses headers that HTTP/1.1 repeats verbatim. And it supports server push (sending files before the client even asks). The catch: one TCP packet loss blocks everything. HTTP/3 fixes that.
Q: What is CORS and why does it exist?
CORS prevents evil-site.com from making requests to your-bank.com using YOUR logged-in cookies. Without it, any website could secretly transfer money from your bank. The browser asks the server "do you allow requests from this other site?" If the server says no, the browser blocks it. This only applies to browsers — server-to-server calls bypass CORS entirely.
Q: Where should you terminate TLS in a microservices setup?
At the edge — your load balancer handles HTTPS with the outside world. Internal traffic between microservices uses plain HTTP (private network, faster). This simplifies certificate management (one cert at the load balancer vs every service needing one). For ultra-sensitive data (banking, healthcare), you might use mutual TLS (mTLS) internally too.
Q: What's a digital certificate and who issues them?
A certificate is like a server's passport — it contains the server's identity and is SIGNED by a trusted Certificate Authority (CA) like Let's Encrypt. Your browser has a pre-installed list of trusted CAs. When the server shows its cert, the browser verifies the CA's signature. If it matches, the server is legit. This prevents attackers from pretending to be Netflix.
Q: Explain HTTP caching headers.
Cache-Control tells browsers and CDNs how to cache: "max-age=3600" (use cached version for 1 hour), "no-cache" (check with server first), "no-store" (never cache, for sensitive data). ETag is a fingerprint — client sends it back, server says "unchanged, use your cache" (304 response). This massively reduces bandwidth and server load.
Quick Quiz
1/5You're on public WiFi at a cafe and log into your bank account. What prevents the person next to you from seeing your password?