Skip to content

Interactive HTTP Request-Response Cycle

Interactive HTTP Request-Response Cycle

HTTP Request-Response Cycle

Ready to send request
API Test+⚠️http://api.example.com/api/usersLoading...Client BrowserDNS ServerDomain LookupRouterISPNetworkISPNetworkWeb ServerONLINELoad: 45%API LayerGET Request - /api/usersDatabasePostgreSQLusers_tableFile System/var/wwwstatic assetsRedis CacheApplication Server⚠️HTTP

HTTP Status Codes Reference

200 OK - Request successful
201 Created - Resource created successfully
302 Found - Resource temporarily moved
400 Bad Request - Invalid request syntax
401 Unauthorized - Authentication required
403 Forbidden - Access denied
404 Not Found - Resource does not exist
500 Internal Server Error - Server malfunction
502 Bad Gateway - Invalid response from upstream
503 Service Unavailable - Server temporarily unavailable

How the Web Works: Client-Server Architecture

Section titled “How the Web Works: Client-Server Architecture”

The World Wide Web operates on a client-server model, where two types of programs communicate with each other:

  • Definition: Programs that request resources from servers
  • Examples:
    • Web browsers (Chrome, Firefox, Safari)
    • Mobile applications
    • API clients and tools (Postman, curl)
    • Web crawlers and bots
  • Definition: Programs that store resources and respond to client requests
  • Examples:
    • Web servers (Apache, Nginx)
    • Application servers (Node.js, PHP, Python)
    • API servers and microservices
    • Content delivery networks (CDNs)
  1. Client makes a request for a resource
  2. Server processes the request
  3. Server sends back a response
  4. Client receives and processes the response

When you click a link or submit a form, here’s what happens step by step:

  • User clicks a link or submits a form
  • Client (browser) constructs an HTTP request
  • Request includes: method, URI, headers, and optional body
  • Server receives the HTTP request
  • Server parses the request components
  • Server determines what resource is being requested
  • Server locates the requested resource
  • For static resources: retrieves file from disk
  • For dynamic resources: executes code, queries database, generates content
  • Server constructs HTTP response
  • Response includes: status code, headers, and body (the actual content)
  • Server sends response back to client
  • Client receives the HTTP response
  • Client renders content (HTML, images, etc.)
  • Client may make additional requests for linked resources (CSS, images, scripts)

A resource is any information that can be named and accessed on the web. Resources are the fundamental concept in HTTP and REST architecture. They represent data or services that can be requested by clients.

Resources are identified by URIs (Uniform Resource Identifiers). A URI is a unique address that points to a specific resource on the web.

A URI consists of several components that work together to uniquely identify a resource:

https://www.example.com:443/products/laptop?color=black&size=15#reviews
└─┬─┘ └──────┬──────┘└┬┘└──────┬──────┘└──────┬──────┘└──┬──┘
scheme host port path query fragment

Components Breakdown:

  • Scheme (https): The protocol used to access the resource (http, https, ftp, etc.)
  • Host (www.example.com): The domain name or IP address of the server
  • Port (:443): The network port (optional, defaults: 80 for HTTP, 443 for HTTPS)
  • Path (/products/laptop): The specific location of the resource on the server
  • Query (?color=black&size=15): Parameters passed to the server (optional)
  • Fragment (#reviews): A specific section within the resource (optional, processed by client)

Examples of URIs:

  • https://api.example.com/users/123 - Points to user with ID 123
  • https://www.example.com/products/laptop - Points to a laptop product page
  • https://api.shop.com/orders/456/items - Points to items in order 456

  • Definition: Content that doesn’t change based on user input or server processing
  • Characteristics: Same content is returned for every request
  • Examples:
    • HTML files (/about.html)
    • Images (/images/logo.png)
    • CSS stylesheets (/styles/main.css)
    • JavaScript files (/scripts/app.js)

  • Definition: Content that is generated or modified based on user input, database queries, or server processing
  • Characteristics: Content may vary depending on request parameters, user authentication, or real-time data
  • Examples:
    • User profiles (/users/profile - shows different data for each logged-in user)
    • Search results (/search?q=laptops - results change based on query)
    • Shopping carts (/cart - contents vary per user session)
    • Live data feeds (/api/stock-prices - real-time financial data)

HTTP (HyperText Transfer Protocol) defines several methods that indicate the desired action to be performed on a resource. The two most commonly used methods are:

  • Purpose: Retrieve data from a server
  • Characteristics: Safe, idempotent, cacheable
  • Use Cases: Fetching web pages, API data retrieval, downloading files
  • Example: GET /users/123 - Retrieve user with ID 123
  • Purpose: Submit data to create a new resource or send data to the server
  • Characteristics: Not safe, not idempotent, not cacheable by default
  • Use Cases: Form submissions, creating new records, file uploads
  • Example: POST /users - Create a new user account

HTTP communication consists of requests (sent by clients) and responses (sent by servers). Both have a specific structure:

GET /users/123 HTTP/1.1 ← Request Line
Host: api.example.com ← Headers
Authorization: Bearer token123
Content-Type: application/json
← Empty line separates headers from body
{ ← Request Body (optional)
"name": "John Doe"
}

Components:

  • Request Line: Method + URI + HTTP Version
  • Headers: Metadata about the request (host, content type, authentication, etc.)
  • Body: Data sent with the request (optional, mainly used with POST)

HTTP/1.1 200 OK ← Status Line
Content-Type: application/json ← Headers
Content-Length: 45
Cache-Control: no-cache
← Empty line separates headers from body
{ ← Response Body
"id": 123,
"name": "John Doe"
}

Components:

  • Status Line: HTTP Version + Status Code + Status Message
  • Headers: Metadata about the response (content type, length, caching rules, etc.)
  • Body: The actual content/data returned by the server

  • 2xx: Success (200 OK, 201 Created)
  • 3xx: Redirection (301 Moved Permanently, 304 Not Modified)
  • 4xx: Client Error (400 Bad Request, 404 Not Found, 401 Unauthorized)
  • 5xx: Server Error (500 Internal Server Error, 503 Service Unavailable)