Skip to content

HTTP 消息示例

本节提供了一些具体的 HTTP 请求和响应交换示例,展示了常见场景以及各种方法和头字段的用法。

客户端请求 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)

示例 2: 发送带 JSON 数据的 POST 请求到 API

Section titled “示例 2: 发送带 JSON 数据的 POST 请求到 API”

客户端发送 JSON 数据到 API 端点以创建新资源。

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
}

示例 3: 请求不存在资源的 GET 请求

Section titled “示例 3: 请求不存在资源的 GET 请求”

客户端请求一个不存在的页面。

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>

示例 4: 使用 ETag 的条件式 GET 请求

Section titled “示例 4: 使用 ETag 的条件式 GET 请求”

客户端请求一个它已缓存的资源,提供 ETag 以检查是否有修改。

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

服务器响应 (304 Not Modified,如果 ETag 匹配):

Section titled “服务器响应 (304 Not Modified,如果 ETag 匹配):”
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)

客户端通过提供其完整的全新表示来更新现有资源。

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 “示例 6: 服务器响应包含多个 Set-Cookie 头字段”

服务器为客户端设置两个 Cookie。

服务器响应 (作为更大响应的一部分,例如登录后):

Section titled “服务器响应 (作为更大响应的一部分,例如登录后):”
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"
}