Mga karaniwang HTTP header
Ang mga HTTP header ay mga key-value pair na ipinapadala sa simula ng bawat request at response. Dala nito ang metadata tulad ng uri ng content, caching rules, at authentication. Gamitin ang sheet na ito para makilala ang pinakakaraniwang header kapag bumubuo o nagde-debug ng web traffic.
| Header | Direksyon | Layunin |
|---|---|---|
| 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. |
Tala
- Ang mga pangalan ng header ay hindi case-sensitive; ayon sa konbensyon, isinusulat ang mga ito sa Title-Case (hal. Content-Type).
- Ang mga request header ay naglalarawan sa client; ang response header naman sa sagot ng server.
- Maraming header (tulad ng Host at Referer) ay awtomatikong tinatakda ng browser — huwag mong pekein.
Mga madalas itanong
- Ano ang pagkakaiba ng request header at response header?
- Ang mga request header ay ipinapadala ng client (browser o app) sa server; ang response header ay ibinabalik ng server kasama ang sagot. Ang ilan, tulad ng Content-Type, ay lumalabas sa parehong panig.
- Ang mga pangalan ba ng HTTP header ay case-sensitive?
- Hindi. Ayon sa spec, ang mga pangalan ng header ay hindi case-sensitive, kaya ang Content-Type at content-type ay iisá lang. Ang Title-Case ay konbensyon lang.
- Para saan ang Host header?
- Sinasabi ng Host sa server kung aling domain ang tinutukoy ng request. Ito ay required sa HTTP/1.1 at nagpapahintulot sa isang server na mag-host ng maraming site ayon sa pangalan.
- Paano gumagana ang caching gamit ang mga header?
- Tinatakda ng Cache-Control kung gaano katagal pwedeng i-cache ang isang response; pinapayagan ng ETag at If-None-Match ang client na magtanong 'nagbago ba?' at tumanggap ng magaang 304 Not Modified.