HTTP 头速查表

HTTP 头速查表

常见请求头 (Request Headers)

Header说明示例
User-Agent客户端信息Mozilla/5.0 (Windows NT 10.0; Win64; x64)...
Accept可接受的内容类型application/json, text/plain, */*
Accept-Language可接受的语言zh-CN,zh;q=0.9,en;q=0.8
Accept-Encoding可接受的编码gzip, deflate, br
Content-Type请求体内容类型application/json; charset=UTF-8
Content-Length请求体长度1234
Authorization认证信息Bearer eyJhbGciOiJIUzI1NiIs...
CookieCookie 数据session_id=abc123; user=admin
Referer来源页面https://example.com/page1
Origin请求来源https://example.com
Host目标主机api.example.com
Connection连接方式keep-alive
Cache-Control缓存控制no-cache, no-store
PragmaHTTP/1.0 缓存控制no-cache
If-Modified-Since条件请求Wed, 21 Oct 2025 07:28:00 GMT
If-None-MatchETag 条件请求"686897696a7c876b7e"
Range范围请求bytes=0-1024
X-Requested-With标识 AJAX 请求XMLHttpRequest
X-CSRF-TokenCSRF 令牌abc123def456

常见响应头 (Response Headers)

Header说明示例
Content-Type响应内容类型application/json; charset=utf-8
Content-Length响应体长度1234
Content-Encoding响应编码gzip
Set-Cookie设置 Cookiesession_id=abc123; Path=/; HttpOnly
Cache-Control缓存策略max-age=3600, must-revalidate
Expires过期时间Wed, 21 Oct 2025 07:28:00 GMT
ETag资源标识"686897696a7c876b7e"
Last-Modified最后修改时间Wed, 21 Oct 2025 07:28:00 GMT
Location重定向地址https://example.com/new-page
Server服务器信息nginx/1.18.0
X-Powered-By技术栈PHP/7.4.0
Access-Control-Allow-OriginCORS 允许来源*https://example.com
Access-Control-Allow-MethodsCORS 允许方法GET, POST, PUT, DELETE
Access-Control-Allow-HeadersCORS 允许头Content-Type, Authorization
Access-Control-Max-AgeCORS 预检缓存3600
Strict-Transport-SecurityHSTSmax-age=31536000; includeSubDomains
X-Frame-Options防点击劫持DENY
X-Content-Type-Options防 MIME 嗅探nosniff
X-XSS-ProtectionXSS 过滤器1; mode=block

安全相关头

请求安全头

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# CSRF 防护
X-CSRF-Token: abc123def456
X-XSRF-TOKEN: abc123def456

# 自定义签名
X-Signature: md5_hash_value
X-Sign: sha256_hash_value
X-Timestamp: 1702887654321

# API密钥
X-API-Key: your_api_key_here
API-Key: your_api_key_here

响应安全头

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 内容安全策略
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline'

# XSS 防护
X-XSS-Protection: 1; mode=block

# 防点击劫持
X-Frame-Options: SAMEORIGIN

# MIME 类型嗅探防护
X-Content-Type-Options: nosniff

# Referer 策略
Referrer-Policy: no-referrer-when-downgrade

# 权限策略
Permissions-Policy: geolocation=(), camera=()

Content-Type 常见值

请求/响应通用

Content-Type说明用途
application/jsonJSON 数据API 请求/响应
application/x-www-form-urlencoded表单数据传统表单提交
multipart/form-data文件上传含文件的表单
text/htmlHTML 文档网页
text/plain纯文本文本文件
text/cssCSS 样式样式表
text/javascriptJavaScriptJS 文件
application/javascriptJavaScriptJS 文件(新标准)
application/xmlXML 数据XML 格式
text/xmlXML 文本XML 文本
application/octet-stream二进制流文件下载
image/jpegJPEG 图片图片
image/pngPNG 图片图片
image/gifGIF 图片动图
image/svg+xmlSVG 图片矢量图
video/mp4MP4 视频视频
audio/mpegMP3 音频音频

自定义头示例

常见的自定义业务头

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 版本控制
X-API-Version: 1.0
X-Client-Version: 2.3.1

# 设备信息
X-Device-ID: 1234567890abcdef
X-Device-Type: mobile
X-Platform: ios
X-OS-Version: 14.5

# 追踪和调试
X-Request-ID: uuid-1234-5678
X-Trace-ID: trace_abc123
X-Debug: true

# 地理位置
X-Geo-Country: CN
X-Geo-City: Beijing
X-Client-IP: 1.2.3.4

# A/B测试
X-Experiment: variant_b
X-Feature-Flag: new_ui_enabled

User-Agent 示例

桌面浏览器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Chrome (Windows)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

# Firefox (Windows)
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0

# Edge (Windows)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0

# Safari (macOS)
Mozilla/5.0 (Macintosh; Intel Mac OS X 14_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15

移动浏览器

1
2
3
4
5
6
7
8
# iPhone Safari
Mozilla/5.0 (iPhone; CPU iPhone OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Mobile/15E148 Safari/604.1

# Android Chrome
Mozilla/5.0 (Linux; Android 13; SM-S908B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36

# iPad Safari
Mozilla/5.0 (iPad; CPU OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Mobile/15E148 Safari/604.1

爬虫/工具

1
2
3
4
5
6
7
8
# Python Requests
python-requests/2.31.0

# Postman
PostmanRuntime/7.36.0

# cURL
curl/7.68.0

Authorization 方式

Bearer Token

1
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Basic Auth

1
2
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
# (username:password 的 Base64编码)

Digest Auth

1
Authorization: Digest username="user", realm="example.com", nonce="abc123", uri="/api", response="def456"

API Key

1
2
3
Authorization: ApiKey your_api_key_here
# 或
X-API-Key: your_api_key_here
1
Set-Cookie: session_id=abc123; Domain=example.com; Path=/; Expires=Wed, 21 Oct 2025 07:28:00 GMT; Max-Age=3600; Secure; HttpOnly; SameSite=Strict

属性说明:

属性说明
DomainCookie 的作用域
PathCookie 的作用路径
Expires过期时间(绝对时间)
Max-Age存活时间(秒)
Secure仅 HTTPS 传输
HttpOnly禁止 JavaScript 访问
SameSite跨站请求策略 (Strict/Lax/None)

Cache-Control 指令

请求指令

1
2
3
4
5
6
Cache-Control: no-cache # 不使用缓存
Cache-Control: no-store # 不存储缓存
Cache-Control: max-age=0 # 立即过期
Cache-Control: max-stale=3600 # 可接受过期的缓存
Cache-Control: min-fresh=600 # 必须新鲜的缓存
Cache-Control: only-if-cached # 只使用缓存

响应指令

1
2
3
4
5
6
7
8
9
Cache-Control: public # 可被任何缓存存储
Cache-Control: private # 只能被浏览器缓存
Cache-Control: no-cache # 需要验证
Cache-Control: no-store # 不能缓存
Cache-Control: max-age=3600 # 缓存3600秒
Cache-Control: s-maxage=3600 # 共享缓存时间
Cache-Control: must-revalidate # 过期后必须验证
Cache-Control: proxy-revalidate # 代理缓存需验证
Cache-Control: immutable # 不会改变

Python 设置 Headers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import requests

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Accept': 'application/json',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Accept-Encoding': 'gzip, deflate',
'Content-Type': 'application/json',
'Authorization': 'Bearer your_token_here',
'X-Requested-With': 'XMLHttpRequest',
'Referer': 'https://example.com',
'Origin': 'https://example.com'
}

response = requests.get('https://api.example.com/data', headers=headers)

JavaScript 设置 Headers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Fetch API
fetch("https://api.example.com/data", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer your_token_here",
"X-Custom-Header": "custom_value",
},
body: JSON.stringify({ key: "value" }),
});

// XHR
const xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.example.com/data");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer your_token_here");
xhr.send(JSON.stringify({ key: "value" }));

cURL 设置 Headers

1
2
3
4
5
6
7
curl https://api.example.com/data \
-H "User-Agent: Mozilla/5.0..." \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_token_here" \
-H "X-Custom-Header: custom_value" \
-d '{"key":"value"}'

常见状态码对照

状态码说明
200OK - 成功
201Created - 已创建
204No Content - 无内容
301Moved Permanently - 永久重定向
302Found - 临时重定向
304Not Modified - 未修改(缓存有效)
400Bad Request - 请求错误
401Unauthorized - 未授权
403Forbidden - 禁止访问
404Not Found - 未找到
405Method Not Allowed - 方法不允许
429Too Many Requests - 请求过多
500Internal Server Error - 服务器错误
502Bad Gateway - 网关错误
503Service Unavailable - 服务不可用

相关章节


相关内容

如果这篇文章对你有帮助,请我喝杯咖啡吧~
+5 支付宝支付宝
+5 微信微信