When you open a webpage in your browser, HTML is usually delivered inside HTTP messages between your browser (client) and the website’s server.
HTTP messages are part of the client-server communication model.


1. What Are HTTP Messages?

HTTP messages are the formatted blocks of data exchanged between:

  • Client → usually your web browser.
  • Server → the machine hosting the website.

There are two main types:

  1. HTTP Request messages → sent by the browser to the server.
  2. HTTP Response messages → sent by the server back to the browser.

2. HTTP Request Messages

A request message asks the server for some resource (e.g., an HTML page, CSS file, image).

Structure:

<request-line>
<headers>
<blank line>

[optional body]

Example – Requesting index.html:

GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/html
  • Request line: GET /index.html HTTP/1.1 → method, path, protocol version.
  • Headers: Metadata like Host, User-Agent, Accept.
  • Body (optional): Only for methods like POST or PUT (e.g., sending form data).

3. HTTP Response Messages

A response message contains the resource (like HTML content) and status information.

Structure:

<status-line>
<headers>
<blank line>

[body]

Example – Server responds with HTML:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 125

<!DOCTYPE html>
<html>
<head><title>Example</title></head>
<body>Hello, World!</body>
</html>
  • Status line: HTTP/1.1 200 OK → protocol version, status code, reason phrase.
  • Headers: Info like content type, length, caching instructions.
  • Body: Actual content (HTML, JSON, image, etc.).

4. Common HTTP Methods in HTML Requests

MethodPurposeExample Use in HTML
GETRetrieve dataLoading a webpage (<a>, <img>, <link>)
POSTSend dataHTML forms (<form method="post">)
PUTUpdate a resourceJavaScript fetch()
DELETERemove a resourceJavaScript fetch()
HEADGet headers onlyChecking if resource exists

5. How HTML Fits into HTTP Messages

  • When you request a webpage, HTML is inside the body of the HTTP response.
  • HTML elements like <form>, <a>, <img>, and <script> trigger new HTTP requests for resources.

Example flow:

  1. Browser sends HTTP request: GET /index.html
  2. Server responds with HTML in the HTTP response body.
  3. Browser parses HTML → sees an <img src="logo.png"> tag → sends another HTTP request for logo.png.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *