Skip to content

HTTP Message Examples

This section provides concrete examples of HTTP request and response exchanges, illustrating common scenarios and the use of various methods and headers.

Client requests the homepage of www.example.com.

GET / HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
HTTP/1.1 200 OK
Date: Wed, 23 Feb 2022 10:00:00 GMT
Server: MyCustomServer/1.0
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Content-Length: 612 (length of the gzipped content)
Last-Modified: Tue, 22 Feb 2022 15:30:00 GMT
ETag: "abcdef123456"
Cache-Control: public, max-age=3600
Connection: keep-alive
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example Domain</title>
</head>
<body>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
</body>
</html>
(Actual content would be gzipped in transit)

Example 2: POST request with JSON data to an API

Section titled “Example 2: POST request with JSON data to an API”

Client sends JSON data to create a new resource at an API endpoint.

POST /api/items HTTP/1.1
Host: api.example.com
User-Agent: MyApiClient/2.1
Content-Type: application/json; charset=utf-8
Content-Length: 35
Accept: application/json
Authorization: Bearer <your_jwt_token_here>
Connection: keep-alive
{
"name": "New Item",
"price": 19.99
}
HTTP/1.1 201 Created
Date: Wed, 23 Feb 2022 10:05:00 GMT
Server: Nginx/1.20.1
Content-Type: application/json; charset=utf-8
Content-Length: 58
Location: /api/items/123
Connection: keep-alive
{
"id": 123,
"name": "New Item",
"price": 19.99
}

Example 3: GET request for a resource that is not found

Section titled “Example 3: GET request for a resource that is not found”

Client requests a non-existent page.

GET /nonexistentpage.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/html,*/*
Connection: keep-alive
HTTP/1.1 404 Not Found
Date: Wed, 23 Feb 2022 10:10:00 GMT
Server: Apache/2.4.52 (Ubuntu)
Content-Type: text/html; charset=iso-8859-1
Content-Length: 289
Connection: close
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /nonexistentpage.html was not found on this server.</p>
<hr>
<address>Apache/2.4.52 (Ubuntu) Server at www.example.com Port 80</address>
</body></html>

Example 4: Conditional GET request using ETag

Section titled “Example 4: Conditional GET request using ETag”

Client requests a resource it has cached, providing the ETag to check for modifications.

GET /styles/main.css HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/css,*/*;q=0.1
If-None-Match: "xyz789abc"
Connection: keep-alive

Server Response (304 Not Modified, if ETag matches):

Section titled “Server Response (304 Not Modified, if ETag matches):”
HTTP/1.1 304 Not Modified
Date: Wed, 23 Feb 2022 10:15:00 GMT
Server: MyCustomServer/1.0
ETag: "xyz789abc"
Cache-Control: public, max-age=86400
Connection: keep-alive
(No message body)

Example 5: PUT request to update a resource

Section titled “Example 5: PUT request to update a resource”

Client updates an existing resource by providing its complete new representation.

PUT /api/items/123 HTTP/1.1
Host: api.example.com
User-Agent: MyApiClient/2.1
Content-Type: application/json; charset=utf-8
Content-Length: 48
Accept: application/json
Authorization: Bearer <your_jwt_token_here>
If-Match: "oldETagValue"
Connection: keep-alive
{
"name": "Updated Item Name",
"price": 24.99
}
HTTP/1.1 200 OK
Date: Wed, 23 Feb 2022 10:20:00 GMT
Server: Nginx/1.20.1
Content-Type: application/json; charset=utf-8
Content-Length: 75
ETag: "newETagValue"
Connection: keep-alive
{
"id": 123,
"name": "Updated Item Name",
"price": 24.99,
"updatedAt": "2022-02-23T10:20:00Z"
}
Section titled “Example 6: Server response with multiple Set-Cookie headers”

Server sets two cookies for the client.

Server Response (part of a larger response, e.g., after login):

Section titled “Server Response (part of a larger response, e.g., after login):”
HTTP/1.1 200 OK
Date: Wed, 23 Feb 2022 10:25:00 GMT
Server: MyAuthServer/1.0
Content-Type: application/json
Set-Cookie: sessionToken=a1b2c3d4e5f6; Path=/; Secure; HttpOnly; SameSite=Lax
Set-Cookie: userPrefs=theme=dark&lang=en; Path=/; Max-Age=31536000; Secure; SameSite=Lax
Connection: keep-alive
{
"status": "Login successful",
"user_id": "user789"
}