常用 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。