一、请求头(Request Headers)完整列表
1. 客户端信息类
| 请求头 | 描述 | 常用值示例 |
|---|
| User-Agent | 标识客户端的浏览器类型、操作系统等信息 | Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 |
| Accept | 指定客户端能够接受的内容类型 | text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 |
| Accept-Language | 指定客户端的语言偏好 | zh-CN,zh;q=0.9,en;q=0.8 |
| Accept-Encoding | 指定客户端能够接受的内容编码方式 | gzip, deflate, br |
| Accept-Charset | 指定客户端接受的字符集 | utf-8, iso-8859-1;q=0.5 |
2. 连接管理类
| 请求头 | 描述 | 常用值示例 |
|---|
| Connection | 连接管理选项 | keep-alive, close |
| Keep-Alive | 连接保持的时间 | timeout=5, max=1000 |
| Upgrade | 升级到其他协议 | websocket |
3. 缓存控制类
| 请求头 | 描述 | 常用值示例 |
|---|
| Cache-Control | 指定缓存机制 | no-cache, max-age=0 |
| If-Match | 比较实体标记(ETag) | "737060cd8c284d8af7ad3082f209582d" |
| If-None-Match | 比较实体标记,用于缓存 | "737060cd8c284d8af7ad3082f209582d" |
| If-Modified-Since | 比较资源的更新时间 | Wed, 21 Oct 2015 07:28:00 GMT |
| If-Unmodified-Since | 比较资源的更新时间 | Wed, 21 Oct 2015 07:28:00 GMT |
| Pragma | HTTP/1.0 的缓存控制 | no-cache |
4. 认证和安全类
| 请求头 | 描述 | 常用值示例 |
|---|
| Authorization | 认证信息 | Bearer eyJhbGciOiJIUzI1NiIs... |
| Cookie | 客户端的 Cookie 信息 | sessionid=abc123; user=john |
| Origin | 请求的来源 | https://www.example.com |
| Referer | 请求的来源页面 | https://www.example.com/page |
5. 内容协商类
| 请求头 | 描述 | 常用值示例 |
|---|
| Content-Type | 请求体的媒体类型 | application/json, multipart/form-data |
| Content-Length | 请求体的长度 | 348 |
| Content-MD5 | 请求体的 MD5 摘要 | Q2hlY2sgSW50ZWdyaXR5IQ== |
| Content-Encoding | 请求体的编码方式 | gzip |
二、常用请求头示例和使用场景
1. 基本请求示例
fetch('https://api.example.com/data', {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer token123'
}
});
2. 文件上传示例
const formData = new FormData();
formData.append('file', fileInput.files[0]);
fetch('https://api.example.com/upload', {
method: 'POST',
headers: {
'Accept': 'application/json',
},
body: formData
});
3. 缓存控制示例
fetch('https://api.example.com/data', {
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'If-None-Match': '"737060cd8c284d8af7ad3082f209582d"'
}
});
三、响应头(Response Headers)完整列表
1. 基础信息类
| 响应头 | 描述 | 常用值示例 |
|---|
| Date | 响应生成的时间戳 | Wed, 21 Oct 2023 07:28:00 GMT |
| Server | 服务器软件信息 | nginx/1.18.0 |
| Connection | 连接状态 | keep-alive |
| Content-Length | 响应体长度 | 348 |
2. 内容类
| 响应头 | 描述 | 常用值示例 |
|---|
| Content-Type | 响应体的媒体类型 | application/json; charset=utf-8 |
| Content-Encoding | 响应体的编码方式 | gzip |
| Content-Language | 响应体的语言 | zh-CN |
| Content-Location | 响应体的实际位置 | /index.html |
3. 缓存控制类
| 响应头 | 描述 | 常用值示例 |
|---|
| Cache-Control | 缓存控制指令 | public, max-age=31536000 |
| ETag | 资源的特定版本标识符 | "33a64df551425fcc55e4d42a148795d9" |
| Expires | 响应过期时间 | Wed, 21 Oct 2024 07:28:00 GMT |
| Last-Modified | 资源最后修改时间 | Wed, 21 Oct 2023 07:28:00 GMT |
4. 安全类
| 响应头 | 描述 | 常用值示例 |
|---|
| Access-Control-Allow-Origin | CORS 允许的源 | * 或 https://example.com |
| Content-Security-Policy | 内容安全策略 | default-src 'self' |
| X-Frame-Options | 框架嵌入控制 | SAMEORIGIN |
| X-XSS-Protection | XSS 防护 | 1; mode=block |
四、常用响应头示例和使用场景
1. 基本 API 响应
app.get('/api/data', (req, res) => {
res.set({
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
'Access-Control-Allow-Origin': '*'
});
res.json({ data: 'example' });
});
2. 静态资源响应
app.get('/static/image.jpg', (req, res) => {
res.set({
'Content-Type': 'image/jpeg',
'Cache-Control': 'public, max-age=31536000',
'ETag': '"33a64df551425fcc55e4d42a148795d9"'
});
res.sendFile('image.jpg');
});
五、使用注意事项
-
安全性考虑
- 敏感信息不要在请求头中明文传输
- 合理设置 CORS 策略
- 使用适当的内容安全策略(CSP)
-
性能优化
- 合理使用缓存控制头
- 考虑使用压缩(Content-Encoding)
- 避免不必要的头部信息
-
兼容性
- 注意旧版浏览器的支持情况
- 某些头部可能需要特定前缀
- 考虑降级处理方案
这个完整指南涵盖了主要的 HTTP 请求头和响应头,以及它们的使用场景和注意事项。在实际开发中,可以根据具体需求选择合适的头部使用。要注意的是,并非所有头部都需要在每个请求中使用,应该根据应用场景选择必要的头部。