HTTP Request and Response Message Format
HTTP Request and Response Message Format
Introduction
HTTP is a request–response protocol used for communication between a web browser (client) and a web server.
- The client sends an HTTP request message to the server.
- The server processes the request and sends an HTTP response message.
Both request and response messages have a well-defined format.
HTTP Message Structure
Both HTTP request and response messages consist of four sections:
| Request Message | Response Message |
|---|---|
| Request Line | Status Line |
| Header Lines | Header Lines |
| Blank Line | Blank Line |
| Body (Optional) | Body (Optional) |
HTTP Request Message
An HTTP request message is sent from the client to the server to request a web page or perform another operation.
Format
Request Line Header Lines Blank Line Body (Optional)
1. Request Line
The request line consists of three fields:
Method URL HTTP Version
Example
GET /index.html HTTP/1.1
Fields
| Field | Description |
|---|---|
| Method | Specifies the type of request |
| URL | Identifies the requested resource |
| Version | Specifies the HTTP version |
HTTP Methods
| Method | Purpose |
|---|---|
| GET | Retrieve a document from the server |
| HEAD | Retrieve only header information |
| PUT | Upload a document to the server |
| POST | Send data to the server |
| TRACE | Echo the received request (debugging) |
| DELETE | Delete a web page from the server |
| CONNECT | Reserved (used mainly by proxy servers) |
| OPTIONS | Request available communication options |
GET is the most commonly used method for retrieving web pages.
2. Header Lines
Header lines carry additional information from the client to the server.
Each header has the format:
Header-Name: Value
Common Request Headers
| Header | Purpose |
|---|---|
| User-Agent | Identifies the browser/client |
| Accept | Media types accepted by the client |
| Accept-Charset | Supported character sets |
| Accept-Encoding | Supported encoding schemes |
| Accept-Language | Preferred language |
| Authorization | Authentication information |
| Host | Server name and port number |
| Date | Current date |
| Upgrade | Preferred communication protocol |
| Cookie | Returns stored cookies |
| If-Modified-Since | Requests file only if modified |
3. Blank Line
A blank line separates the header section from the body.
4. Body (Optional)
The body contains data sent by the client.
It is mainly used with:
- PUT
- POST
Examples:
- Form data
- Uploaded files
- Comments
For a GET request, the body is usually empty.
HTTP Response Message
An HTTP response message is sent by the server after processing the client's request.
Format
Status Line Header Lines Blank Line Body (Optional)
1. Status Line
The status line consists of three fields:
HTTP Version Status Code Status Phrase
Example
HTTP/1.1 200 OK
Fields
| Field | Description |
|---|---|
| Version | HTTP version used |
| Status Code | Indicates the result of the request |
| Status Phrase | Text description of the status code |
HTTP Status Codes
| Code Range | Meaning |
|---|---|
| 100–199 | Informational messages |
| 200–299 | Successful request |
| 300–399 | Redirection |
| 400–499 | Client-side errors |
| 500–599 | Server-side errors |
Examples
| Code | Meaning |
|---|---|
| 200 OK | Request successful |
| 301 Moved Permanently | Resource moved |
| 404 Not Found | Requested page not found |
| 500 Internal Server Error | Server error |
2. Header Lines
Response headers provide additional information about the server and the returned document.
Each header has the format:
Header-Name: Value
Common Response Headers
| Header | Purpose |
|---|---|
| Date | Current date and time |
| Server | Server software information |
| Content-Type | Type of document returned |
| Content-Length | Size of the document |
| Content-Language | Language of the document |
| Content-Encoding | Encoding scheme used |
| Last-Modified | Date of last modification |
| Location | Redirects the client to another URL |
| Set-Cookie | Sends cookies to the client |
| Accept-Ranges | Indicates supported byte ranges |
| Upgrade | Preferred communication protocol |
3. Blank Line
Separates the header section from the body.
4. Body (Optional)
The body contains the actual resource requested by the client.
Examples:
- HTML page
- Image
- PDF document
- Audio or video file
The body is absent when:
- The response is only header information (e.g., HEAD request)
- An error response contains no resource
Example
Example
Example
Conditional Request
Comparison of Request and Response Messages
| Feature | Request Message | Response Message |
|---|---|---|
| Sent By | Client | Server |
| First Line | Request Line | Status Line |
| Contains | Method, URL, Version | Version, Status Code, Status Phrase |
| Header Lines | Client information | Server and document information |
| Body | Optional (mainly POST/PUT) | Requested document or error information |
Summary
In HTTP communication, the client sends a request message to ask for a resource, and the server returns a response message containing the requested resource or an error message. Both messages consist of four sections: a request/status line, header lines, a blank line, and an optional body. The request line specifies the HTTP method, URL, and version, while the status line contains the HTTP version, status code, and status phrase indicating the result of the request.



Comments
Post a Comment