HTTP 消息示例
HTTP - 消息示例
Section titled “HTTP - 消息示例”本节提供了一些具体的 HTTP 请求和响应交换示例,展示了常见场景以及各种方法和头字段的用法。
示例 1: GET 请求获取 HTML 页面
Section titled “示例 1: GET 请求获取 HTML 页面”客户端请求 www.example.com 的首页。
客户端请求:
Section titled “客户端请求:”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: 1服务器响应 (成功):
Section titled “服务器响应 (成功):”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)示例 2: 发送带 JSON 数据的 POST 请求到 API
Section titled “示例 2: 发送带 JSON 数据的 POST 请求到 API”客户端发送 JSON 数据到 API 端点以创建新资源。
客户端请求:
Section titled “客户端请求:”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}服务器响应 (资源已创建):
Section titled “服务器响应 (资源已创建):”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}示例 3: 请求不存在资源的 GET 请求
Section titled “示例 3: 请求不存在资源的 GET 请求”客户端请求一个不存在的页面。
客户端请求:
Section titled “客户端请求:”GET /nonexistentpage.html HTTP/1.1Host: www.example.comUser-Agent: Mozilla/5.0Accept: text/html,*/*Connection: keep-alive服务器响应 (404 Not Found):
Section titled “服务器响应 (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>示例 4: 使用 ETag 的条件式 GET 请求
Section titled “示例 4: 使用 ETag 的条件式 GET 请求”客户端请求一个它已缓存的资源,提供 ETag 以检查是否有修改。
客户端请求:
Section titled “客户端请求:”GET /styles/main.css HTTP/1.1Host: www.example.comUser-Agent: Mozilla/5.0Accept: text/css,*/*;q=0.1If-None-Match: "xyz789abc"Connection: keep-alive服务器响应 (304 Not Modified,如果 ETag 匹配):
Section titled “服务器响应 (304 Not Modified,如果 ETag 匹配):”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)示例 5: 使用 PUT 请求更新资源
Section titled “示例 5: 使用 PUT 请求更新资源”客户端通过提供其完整的全新表示来更新现有资源。
客户端请求:
Section titled “客户端请求:”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}服务器响应 (更新成功):
Section titled “服务器响应 (更新成功):”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"}示例 6: 服务器响应包含多个 Set-Cookie 头字段
Section titled “示例 6: 服务器响应包含多个 Set-Cookie 头字段”服务器为客户端设置两个 Cookie。
服务器响应 (作为更大响应的一部分,例如登录后):
Section titled “服务器响应 (作为更大响应的一部分,例如登录后):”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"}