How the Web Works: HTTP, REST & APIs
When you type a URL and press Enter, a dozen protocols coordinate in milliseconds. Senior engineers can trace this journey from DNS lookup to rendered pixels β and more importantly, they know where performance and security problems hide in each step.
What Happens When You Type a URL
1. URL Parsing β Browser parses scheme, host, path, query, fragment
2. DNS Resolution β Domain name β IP address
3. TCP Connection β Three-way handshake (SYN β SYN-ACK β ACK)
4. TLS Handshake β Certificate verification, key exchange (HTTPS)
5. HTTP Request β Browser sends GET request with headers
6. Server Processing β Server generates response
7. HTTP Response β Status code, headers, body
8. Parsing & Rendering β HTML β DOM β CSSOM β Layout β Paint β CompositeDNS Resolution
Browser cache β OS cache β Router cache β ISP DNS β Root DNS β TLD DNS β Authoritative DNS
β
IP: 93.184.216.34Each step is a potential cache hit. First visits resolve the full chain; subsequent visits hit local caches. dns-prefetch and preconnect hints move this cost earlier:
<link rel="dns-prefetch" href="https://api.example.com" />
<link rel="preconnect" href="https://cdn.example.com" crossorigin />TCP Three-Way Handshake
Client Server
βββ SYN βββββββββββββββββββΆβ 1. Client requests connection
ββββββββββββ SYN-ACK βββββββ 2. Server acknowledges + requests back
βββ ACK βββββββββββββββββββΆβ 3. Client confirms β connection openEach round trip adds latency. HTTP/2 multiplexes over one connection; HTTP/3 (QUIC) runs over UDP to eliminate head-of-line blocking.
TLS Handshake (HTTPS)
Client Server
βββ ClientHello (ciphers, TLS version) βββΆβ
ββββ ServerHello (chosen cipher, cert) ββββ
βββ Verify cert, exchange keys βββββββββββΆβ
ββββ Finished ββββββββββββββββββββββββββββ
β π Encrypted connection established βTLS 1.3 reduces this to one round trip (vs two in TLS 1.2). The certificate chain is verified against the browser's trusted CA store.
HTTP Fundamentals
HTTP Methods
| Method | Purpose | Idempotent | Safe | Body |
|---|---|---|---|---|
GET | Retrieve a resource | Yes | Yes | No |
POST | Create a resource / submit data | No | No | Yes |
PUT | Replace a resource entirely | Yes | No | Yes |
PATCH | Partially update a resource | No* | No | Yes |
DELETE | Remove a resource | Yes | No | Optional |
HEAD | Same as GET but no response body | Yes | Yes | No |
OPTIONS | Discover supported methods (CORS preflight) | Yes | Yes | No |
Idempotent: Multiple identical requests produce the same result. PUT /users/1 with the same body always results in the same state. POST /users creates a new user each time.
Safe: Doesn't modify server state. GET and HEAD should never have side effects.
HTTP Status Codes
| Range | Category | Common Codes |
|---|---|---|
| 1xx | Informational | 101 Switching Protocols (WebSocket upgrade) |
| 2xx | Success | 200 OK, 201 Created, 204 No Content |
| 3xx | Redirection | 301 Moved Permanently, 302 Found, 304 Not Modified |
| 4xx | Client Error | 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 409 Conflict, 422 Unprocessable Entity, 429 Too Many Requests |
| 5xx | Server Error | 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout |
HTTP Headers
Request Headers:
GET /api/users HTTP/1.1
Host: api.example.com
Accept: application/json
Authorization: Bearer eyJhbGc...
Content-Type: application/json
Cache-Control: no-cache
User-Agent: Mozilla/5.0 ...
Accept-Encoding: gzip, br
Cookie: session=abc123
Origin: https://app.example.comResponse Headers:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 1234
Cache-Control: public, max-age=3600, stale-while-revalidate=86400
ETag: "abc123"
Set-Cookie: session=xyz; HttpOnly; Secure; SameSite=Strict
Access-Control-Allow-Origin: https://app.example.com
Content-Encoding: br
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniffKey Security Headers:
| Header | Purpose |
|---|---|
Strict-Transport-Security (HSTS) | Force HTTPS for all future requests |
Content-Security-Policy (CSP) | Restrict where scripts/styles/images can load from |
X-Content-Type-Options: nosniff | Prevent MIME-type sniffing |
X-Frame-Options | Prevent clickjacking via iframes |
Referrer-Policy | Control what's sent in the Referer header |
Permissions-Policy | Restrict browser features (camera, geolocation) |
Caching Headers
Cache-Control: public, max-age=31536000, immutable
β CDN and browser can cache for 1 year, never revalidate
Cache-Control: private, no-cache
β Only browser can cache, must revalidate with server every time
Cache-Control: no-store
β Never cache (sensitive data)
ETag: "v1.2.3"
β Server sends a version tag; browser sends If-None-Match on next request
β Server responds 304 Not Modified if unchanged (saves bandwidth)HTTPS
HTTPS = HTTP + TLS encryption. It provides:
- Confidentiality β Data encrypted in transit (can't be read by intermediaries)
- Integrity β Data can't be modified without detection
- Authentication β Certificate proves server identity
Modern browsers flag HTTP sites as "Not Secure." Service Workers, Geolocation, and other sensitive APIs require HTTPS.
REST API Design
REST (Representational State Transfer) is a set of conventions for API design:
Resource-Based URLs
GET /api/users β List all users
POST /api/users β Create a user
GET /api/users/42 β Get user 42
PUT /api/users/42 β Replace user 42
PATCH /api/users/42 β Update user 42 partially
DELETE /api/users/42 β Delete user 42
GET /api/users/42/posts β List user 42's posts (nested resource)REST Principles
- Stateless β Each request contains all information needed; no server-side session
- Resource-based β URLs represent nouns (resources), not verbs
- Uniform interface β Standard HTTP methods for CRUD operations
- Client-server separation β Frontend and backend evolve independently
- Cacheable β Responses declare cacheability via headers
REST vs Other API Styles
| Feature | REST | GraphQL | gRPC |
|---|---|---|---|
| Protocol | HTTP | HTTP (typically POST) | HTTP/2 |
| Data format | JSON (usually) | JSON | Protocol Buffers |
| Fetching | Fixed structure per endpoint | Client specifies shape | Method-based RPC |
| Over-fetching | Common (fixed responses) | Eliminated (query what you need) | N/A |
| Under-fetching | Requires multiple requests | Single query | N/A |
| Caching | HTTP caching (simple) | Complex (query-based) | Limited |
| Real-time | Polling / WebSocket | Subscriptions | Streaming |
| Best for | Simple CRUD, public APIs | Complex data graphs, mobile | Microservice-to-microservice |
SOAP (Legacy)
SOAP uses XML for request/response with a strict contract (WSDL). It's largely replaced by REST/GraphQL in modern web development but still exists in enterprise/banking systems:
<soap:Envelope>
<soap:Body>
<GetUser>
<UserId>42</UserId>
</GetUser>
</soap:Body>
</soap:Envelope>REST is preferred for web frontends because:
- Simpler (JSON vs XML)
- Cacheable (HTTP-native caching)
- Lighter payloads
- Works naturally with browser fetch API
The Complete Request Lifecycle
User clicks link
β
βΌ
Browser checks caches (Service Worker β HTTP cache β DNS cache)
β
βΌ
DNS resolution (if cache miss)
β
βΌ
TCP + TLS connection (or reuse existing)
β
βΌ
HTTP request with headers
β
βΌ
Server processes request
β
βΌ
HTTP response with status + headers + body
β
βΌ
Browser parses response
β
βββ HTML β DOM tree
βββ CSS β CSSOM
βββ JS β Execute (may modify DOM)
β
βΌ
Layout β Paint β Composite β Pixels on screenInterview Signal
Senior candidates demonstrate:
- Full journey β Can trace URL to pixels: DNS β TCP β TLS β HTTP β Parse β Render, with performance implications at each step
- HTTP method semantics β Idempotency, safety, correct method for each CRUD operation
- Header fluency β Cache-Control directives, security headers, CORS headers, ETag/If-None-Match
- REST conventions β Resource-based URLs, proper status codes, statelessness, when to choose GraphQL/gRPC instead
- HTTPS understanding β What TLS provides (confidentiality, integrity, authentication), why HTTP is insufficient