常用 HTTP 请求头
HTTP 请求头是每个请求与响应开头发送的键值对。它们携带内容类型、缓存规则、身份认证等元数据。本表帮你识别在构建或调试网络流量时最常见的请求头。
| 请求头 | 方向 | 用途 |
|---|---|---|
| Host | request | Identifies the server's host and port (from the URL). |
| User-Agent | request | Reports the client software (browser, OS, bot) making the request. |
| Accept | request | Lists the response media types (MIME) the client can handle. |
| Authorization | request | Carries credentials (token, Basic auth) to authenticate the client. |
| Content-Type | request/response | States the media type of the request or response body. |
| Content-Length | request/response | Gives the size in bytes of the request or response body. |
| Cookie | request | Sends stored key/value pairs from a previous response back to the server. |
| Set-Cookie | response | Server instructs the client to store a cookie for later requests. |
| Cache-Control | request/response | Directs caching behaviour: no-cache, max-age, private, etc. |
| ETag | response | Opaque identifier for a resource version, used for revalidation. |
| If-None-Match | request | Sends an ETag; server returns 304 if the resource is unchanged. |
| Location | response | Points the client to a new URL for redirects (3xx) or created resources. |
| Access-Control-Allow-Origin | response | Permits a browser to share a response across origins (CORS). |
| X-Forwarded-For | request | Records the client IP as seen through a proxy or load balancer. |
| Referer | request | Indicates the URL of the page that linked to the requested resource. |
说明
- 请求头名称不区分大小写,习惯性写成 Title-Case(如 Content-Type)。
- 请求头描述客户端,响应头描述服务器的回答。
- 浏览器会自动设置许多请求头(如 Host、Referer),不应伪造。
常见问题
- 请求头和响应头有什么区别?
- 请求头由客户端(浏览器或应用)发往服务器;响应头由服务器随答案返回。部分头如 Content-Type 两侧都可能出现。
- HTTP 请求头名称区分大小写吗?
- 不区分。按规范名称大小写无关,Content-Type 与 content-type 含义相同。Title-Case 仅为约定写法。
- Host 请求头有什么用?
- Host 告知服务器请求指向哪个域名。HTTP/1.1 中必填,使一台服务器可按名称托管多个网站。
- 缓存如何用请求头实现?
- Cache-Control 规定响应可缓存多久;ETag 与 If-None-Match 让客户端询问“内容变了吗?”,未变则返回轻量的 304。