In previous chapters, we learned the basic concepts of the HTTP protocol. Today, we will explore HTTP messages, using the analogy of mailing a letter to help you easily understand HTTP messages.
Core Analogy: HTTP is Like Mailing a Letter
Imagine you want to send a letter and a gift to a friend through the post office.
- HTTP is the set of mailing rules that dictate how to write the envelope and how to place the letter inside.
- Message is the complete mail, which consists of two parts: the envelope (message header) and the content of the letter (message body).
This “mail” has two types: the letter you send (request) and the letter your friend sends back to you (response).
1. Request Message – The “Letter” You Send to the Server
When you enter a URL in the browser, click a link, or submit a form, the browser sends a “request letter” to the server.
This letter contains:
A. Start Line (the most important information on the front of the envelope) This is like writing “What I want you to do” on the envelope. It consists of three parts:
- Method (Action): What do you want to do?
<span>GET: </span><strong><span>Retrieve</span></strong><span> something. (Saying: Server, show me that page.)</span><span>POST: </span><strong><span>Submit</span></strong><span> something. (Saying: Server, here is the form I filled out/uploaded file, please accept it.)</span><span>PUT/DELETE: </span><strong><span>Update</span></strong><span> or </span><strong><span>Delete</span></strong><span> something. (Similar to modifying or deleting files on a cloud drive)</span>- URL (Address): Where is the thing you want? (For example:
<span>/products/123</span>) - HTTP Version: The version of the communication rules used (e.g., HTTP/1.1).
Example: <span>GET /index.html HTTP/1.1</span> (Meaning: Using HTTP/1.1 rules, retrieve the <span>/index.html</span> file.)
B. Request Headers (Other labels and instructions on the envelope) This is like various labels you put on the envelope to tell the post office how to handle this letter. It consists of key-value pairs of <span>Name: Value</span>.
<span><span>Host: www.example.com: Where should the letter be sent? (Virtual host identification)</span></span><span><span>User-Agent: Mozilla/5.0...: What browser I used to send the letter.</span></span><span><span>Accept: text/html: I hope to receive a reply in HTML format.</span></span><span><span>Content-Type: application/json: The content I wrote on the letter is in JSON format. (Only needed when there is a body in POST/PUT requests)</span></span><span><span>Authorization: Bearer ...: This is my identity credential/password.</span></span>
C. Blank Line A blank line separates the “envelope” from the “letter”. (Very important, indicating the end of the header information.)
D. Request Body (The specific content in the letter) This is the specific content of the letter.
- GET requests usually do not have a body because your intention (to retrieve) is already clearly stated in the start line.
- POST or PUT requests have a body, which contains the data you want to submit, such as the username and password filled in the form, or the content of the file to be uploaded.
An example of a complete request message:
POST /login HTTP/1.1 // Start line: I want to submit data to /login
Host: www.example.com // Request header: Send to which website
User-Agent: Chrome/91.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 29
// Blank line
username=abc&password=123456 // Request body: Specific data submitted
2. Response Message – The “Letter” the Server Sends Back to You
After the server receives your request, it will send you a reply letter.
A. Status Line (The most important information on the front of the reply envelope) This is like your friend writing on the reply envelope “How did it go?”. It also consists of three parts:
- HTTP Version
- Status Code (a three-digit number): Tells you the result of the request.
<span>200 OK</span>Success! The thing you wanted is in the letter.<span>404 Not Found</span>I couldn’t find what you wanted. (For example, you entered the wrong URL)<span>500 Internal Server Error</span>I (the server) made a mistake.<span>302 Found</span>The thing you wanted is not here, go find it at another address. (Redirect)<span>401 Unauthorized</span>You do not have permission to access this.- Status Text: A brief textual description of the status code.
Example: <span>HTTP/1.1 200 OK</span> (Meaning: Reply according to HTTP/1.1 rules, everything is done!)
B. Response Headers (Other labels on the reply envelope) Similarly, it consists of key-value pairs of <span>Name: Value</span> that tell you how to handle this reply.
<span>Content-Type: text/html; charset=utf-8</span>The content of the reply letter is in HTML format, encoded in UTF-8.<span>Content-Length: 1024</span>The length of the reply letter is 1024 bytes.<span>Set-Cookie: sessionid=abc123...</span>Gives you a “membership card” (Cookie), remember to bring it next time.
C. Blank Line Similarly, a blank line separates the header and body.
D. Response Body (The content of the reply letter) This is what you really want!
- If the request was for a webpage, here is the HTML code.
- If the request was for an image, here is the binary data of the image.
- If the request was for API data, here may be data in JSON or XML format.
An example of a complete response message:
HTTP/1.1200 OK // Status line: Success!
Content-Type: text/html // Response header: Content is HTML webpage
Content-Length: 135
Set-Cookie: user=abc
// Blank line
<html> // Response body: HTML code of the webpage
<body>
<h1>Welcome back!</h1>
</body>
</html>
Summary
| HTTP Message Part | Mailing Analogy | Function |
|---|---|---|
| Start Line/Status Line | The core information on the front of the envelope (action/result) | Indicates the intention or result of this communication |
| Headers | Various labels on the envelope | Describes metadata, such as content type, length, server information, etc. |
| Blank Line | Separation between the envelope and the letter | Format requirement, indicating the end of the header information |
| Body | The specific content in the letter | Holds the actual data (such as form data, HTML, images, etc.) |
In simple terms, HTTP messages are notes exchanged between the client and server, stating “What do you want?”, “What is the result?”, and “What is the specific content?”.