HTTP Message Examples
HTTP - Message Examples
Section titled “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.
Example 1: GET request for an HTML page
Section titled “Example 1: GET request for an HTML page”Client requests the homepage of www.example.com.
Client Request:
Section titled “Client Request:”GET / HTTP/1.1Host: www.example.comUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8Accept-Language: en-US,en;q=0.9Accept-Encoding: gzip, deflate, brConnection: keep-aliveUpgrade-Insecure-Requests: 1Server Response (Successful):
Section titled “Server Response (Successful):”HTTP/1.1 200 OKDate: Wed, 23 Feb 2022 10:00:00 GMTServer: MyCustomServer/1.0Content-Type: text/html; charset=utf-8Content-Encoding: gzipContent-Length: 612 (length of the gzipped content)Last-Modified: Tue, 22 Feb 2022 15:30:00 GMTETag: "abcdef123456"Cache-Control: public, max-age=3600Connection: 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.
Client Request:
Section titled “Client Request:”POST /api/items HTTP/1.1Host: api.example.comUser-Agent: MyApiClient/2.1Content-Type: application/json; charset=utf-8Content-Length: 35Accept: application/jsonAuthorization: Bearer <your_jwt_token_here>Connection: keep-alive
{ "name": "New Item", "price": 19.99}Server Response (Resource Created):
Section titled “Server Response (Resource Created):”HTTP/1.1 201 CreatedDate: Wed, 23 Feb 2022 10:05:00 GMTServer: Nginx/1.20.1Content-Type: application/json; charset=utf-8Content-Length: 58Location: /api/items/123Connection: 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.
Client Request:
Section titled “Client Request:”GET /nonexistentpage.html HTTP/1.1Host: www.example.comUser-Agent: Mozilla/5.0Accept: text/html,*/*Connection: keep-aliveServer Response (404 Not Found):
Section titled “Server Response (404 Not Found):”HTTP/1.1 404 Not FoundDate: Wed, 23 Feb 2022 10:10:00 GMTServer: Apache/2.4.52 (Ubuntu)Content-Type: text/html; charset=iso-8859-1Content-Length: 289Connection: 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.
Client Request:
Section titled “Client Request:”GET /styles/main.css HTTP/1.1Host: www.example.comUser-Agent: Mozilla/5.0Accept: text/css,*/*;q=0.1If-None-Match: "xyz789abc"Connection: keep-aliveServer Response (304 Not Modified, if ETag matches):
Section titled “Server Response (304 Not Modified, if ETag matches):”HTTP/1.1 304 Not ModifiedDate: Wed, 23 Feb 2022 10:15:00 GMTServer: MyCustomServer/1.0ETag: "xyz789abc"Cache-Control: public, max-age=86400Connection: 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.
Client Request:
Section titled “Client Request:”PUT /api/items/123 HTTP/1.1Host: api.example.comUser-Agent: MyApiClient/2.1Content-Type: application/json; charset=utf-8Content-Length: 48Accept: application/jsonAuthorization: Bearer <your_jwt_token_here>If-Match: "oldETagValue"Connection: keep-alive
{ "name": "Updated Item Name", "price": 24.99}Server Response (Successful Update):
Section titled “Server Response (Successful Update):”HTTP/1.1 200 OKDate: Wed, 23 Feb 2022 10:20:00 GMTServer: Nginx/1.20.1Content-Type: application/json; charset=utf-8Content-Length: 75ETag: "newETagValue"Connection: keep-alive
{ "id": 123, "name": "Updated Item Name", "price": 24.99, "updatedAt": "2022-02-23T10:20:00Z"}Example 6: Server response with multiple Set-Cookie headers
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 OKDate: Wed, 23 Feb 2022 10:25:00 GMTServer: MyAuthServer/1.0Content-Type: application/jsonSet-Cookie: sessionToken=a1b2c3d4e5f6; Path=/; Secure; HttpOnly; SameSite=LaxSet-Cookie: userPrefs=theme=dark&lang=en; Path=/; Max-Age=31536000; Secure; SameSite=LaxConnection: keep-alive
{ "status": "Login successful", "user_id": "user789"}