HTTP Messages

Understanding HTTP requests and responses

📡 What are HTTP Messages?

HTTP messages are how web browsers and servers communicate. They include requests (from browser to server) and responses (from server to browser).


<!-- When you click a link, browser sends HTTP request -->
<a href="page.html">Click me</a>

<!-- Server responds with HTTP message containing HTML -->
HTTP/1.1 200 OK
Content-Type: text/html

<html><body>Hello!</body></html>
                                    

HTTP Status Codes

2xx Success

Request was successful

200 OK - Success
201 Created - New resource
204 No Content - Success, no data
🔄

3xx Redirection

Further action needed

301 Moved Permanently
302 Found (Temporary)
304 Not Modified

4xx Client Error

Problem with the request

400 Bad Request
401 Unauthorized
404 Not Found
🔥

5xx Server Error

Server failed to fulfill request

500 Internal Server Error
502 Bad Gateway
503 Service Unavailable

🔹 HTTP Request Structure

Every HTTP request has these components:

GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Accept: text/html,application/xhtml+xml
Accept-Language: en-US,en;q=0.9
Connection: keep-alive

[Optional request body]

Request Components:

  • Request Line: Method, URL, HTTP version
  • Headers: Additional information about request
  • Body: Data sent with request (optional)

🔹 HTTP Response Structure

Server responses follow this format:

HTTP/1.1 200 OK
Date: Mon, 23 May 2024 22:38:34 GMT
Server: Apache/2.4.1 (Unix)
Content-Type: text/html; charset=UTF-8
Content-Length: 88
Connection: close

<html>
<body>
    <h1>Hello World!</h1>
</body>
</html>

Response Components:

  • Status Line: HTTP version, status code, reason phrase
  • Headers: Information about response
  • Body: The actual content (HTML, CSS, JS, etc.)

🔹 Common Status Codes Explained

Understanding what different status codes mean:

🔸 Success Codes (2xx)

  • 200 OK: Request successful, content returned
  • 201 Created: New resource was created successfully
  • 204 No Content: Success but no content to return

🔸 Redirection Codes (3xx)

  • 301 Moved Permanently: Resource moved to new URL forever
  • 302 Found: Resource temporarily at different URL
  • 304 Not Modified: Use cached version

🔸 Client Error Codes (4xx)

  • 400 Bad Request: Invalid request syntax
  • 401 Unauthorized: Authentication required
  • 403 Forbidden: Access denied
  • 404 Not Found: Resource doesn't exist

🔸 Server Error Codes (5xx)

  • 500 Internal Server Error: Generic server error
  • 502 Bad Gateway: Invalid response from upstream server
  • 503 Service Unavailable: Server temporarily overloaded

🔹 HTTP Headers

Headers provide additional information about requests and responses:

🔸 Common Request Headers

Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Accept: text/html,application/xhtml+xml,application/xml
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cookie: sessionid=abc123; theme=dark

🔸 Common Response Headers

Content-Type: text/html; charset=UTF-8
Content-Length: 1234
Set-Cookie: sessionid=xyz789; Path=/; HttpOnly
Cache-Control: max-age=3600
Location: https://www.example.com/new-page
Server: Apache/2.4.41

🧠 Test Your Knowledge

What does HTTP status code 404 mean?