DNA🌐 Web BrowserHow the Web Works: HTTP, REST & APIs
🐣HatchlingBrowserHTTPRESTNetworkingFundamentals

How the Web Works: HTTP, REST & APIs

From URL to pixels β€” understanding DNS resolution, TCP/TLS handshakes, HTTP methods, REST conventions, and request/response headers is foundational knowledge every senior engineer must articulate.

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 β†’ Composite

DNS Resolution

Browser cache β†’ OS cache β†’ Router cache β†’ ISP DNS β†’ Root DNS β†’ TLD DNS β†’ Authoritative DNS
                                                                              ↓
                                                                      IP: 93.184.216.34

Each 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 open

Each 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

MethodPurposeIdempotentSafeBody
GETRetrieve a resourceYesYesNo
POSTCreate a resource / submit dataNoNoYes
PUTReplace a resource entirelyYesNoYes
PATCHPartially update a resourceNo*NoYes
DELETERemove a resourceYesNoOptional
HEADSame as GET but no response bodyYesYesNo
OPTIONSDiscover supported methods (CORS preflight)YesYesNo

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

RangeCategoryCommon Codes
1xxInformational101 Switching Protocols (WebSocket upgrade)
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirection301 Moved Permanently, 302 Found, 304 Not Modified
4xxClient Error400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 409 Conflict, 422 Unprocessable Entity, 429 Too Many Requests
5xxServer Error500 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.com

Response 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: nosniff

Key Security Headers:

HeaderPurpose
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: nosniffPrevent MIME-type sniffing
X-Frame-OptionsPrevent clickjacking via iframes
Referrer-PolicyControl what's sent in the Referer header
Permissions-PolicyRestrict 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:

  1. Confidentiality β€” Data encrypted in transit (can't be read by intermediaries)
  2. Integrity β€” Data can't be modified without detection
  3. 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

  1. Stateless β€” Each request contains all information needed; no server-side session
  2. Resource-based β€” URLs represent nouns (resources), not verbs
  3. Uniform interface β€” Standard HTTP methods for CRUD operations
  4. Client-server separation β€” Frontend and backend evolve independently
  5. Cacheable β€” Responses declare cacheability via headers

REST vs Other API Styles

FeatureRESTGraphQLgRPC
ProtocolHTTPHTTP (typically POST)HTTP/2
Data formatJSON (usually)JSONProtocol Buffers
FetchingFixed structure per endpointClient specifies shapeMethod-based RPC
Over-fetchingCommon (fixed responses)Eliminated (query what you need)N/A
Under-fetchingRequires multiple requestsSingle queryN/A
CachingHTTP caching (simple)Complex (query-based)Limited
Real-timePolling / WebSocketSubscriptionsStreaming
Best forSimple CRUD, public APIsComplex data graphs, mobileMicroservice-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 screen

Interview Signal

Senior candidates demonstrate:

  1. Full journey β€” Can trace URL to pixels: DNS β†’ TCP β†’ TLS β†’ HTTP β†’ Parse β†’ Render, with performance implications at each step
  2. HTTP method semantics β€” Idempotency, safety, correct method for each CRUD operation
  3. Header fluency β€” Cache-Control directives, security headers, CORS headers, ETag/If-None-Match
  4. REST conventions β€” Resource-based URLs, proper status codes, statelessness, when to choose GraphQL/gRPC instead
  5. HTTPS understanding β€” What TLS provides (confidentiality, integrity, authentication), why HTTP is insufficient