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