常用 HTTP 请求头

HTTP 请求头是每个请求与响应开头发送的键值对。它们携带内容类型、缓存规则、身份认证等元数据。本表帮你识别在构建或调试网络流量时最常见的请求头。

更新日期:

请求头方向用途
HostrequestIdentifies the server's host and port (from the URL).
User-AgentrequestReports the client software (browser, OS, bot) making the request.
AcceptrequestLists the response media types (MIME) the client can handle.
AuthorizationrequestCarries credentials (token, Basic auth) to authenticate the client.
Content-Typerequest/responseStates the media type of the request or response body.
Content-Lengthrequest/responseGives the size in bytes of the request or response body.
CookierequestSends stored key/value pairs from a previous response back to the server.
Set-CookieresponseServer instructs the client to store a cookie for later requests.
Cache-Controlrequest/responseDirects caching behaviour: no-cache, max-age, private, etc.
ETagresponseOpaque identifier for a resource version, used for revalidation.
If-None-MatchrequestSends an ETag; server returns 304 if the resource is unchanged.
LocationresponsePoints the client to a new URL for redirects (3xx) or created resources.
Access-Control-Allow-OriginresponsePermits a browser to share a response across origins (CORS).
X-Forwarded-ForrequestRecords the client IP as seen through a proxy or load balancer.
RefererrequestIndicates the URL of the page that linked to the requested resource.

说明

常见问题

请求头和响应头有什么区别?
请求头由客户端(浏览器或应用)发往服务器;响应头由服务器随答案返回。部分头如 Content-Type 两侧都可能出现。
HTTP 请求头名称区分大小写吗?
不区分。按规范名称大小写无关,Content-Type 与 content-type 含义相同。Title-Case 仅为约定写法。
Host 请求头有什么用?
Host 告知服务器请求指向哪个域名。HTTP/1.1 中必填,使一台服务器可按名称托管多个网站。
缓存如何用请求头实现?
Cache-Control 规定响应可缓存多久;ETag 与 If-None-Match 让客户端询问“内容变了吗?”,未变则返回轻量的 304。