Popularne nagłówki HTTP
Nagłówki HTTP to pary klucz-wartość wysyłane na początku każdego żądania i odpowiedzi. Przenoszą metadane, takie jak typ treści, reguły cache i uwierzytelnianie. Użyj tej listy, by rozpoznawać najczęstsze nagłówki przy tworzeniu lub debugowaniu ruchu sieciowego.
| Nagłówek | Kierunek | Przeznaczenie |
|---|---|---|
| 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. |
Uwagi
- Nazwy nagłówków nie rozróżniają wielkości liter; zwyczajowo pisze się je w Title-Case (np. Content-Type).
- Nagłówki żądania opisują klienta, nagłówki odpowiedzi — odpowiedź serwera.
- Przeglądarka ustawia wiele nagłówków (jak Host i Referer) automatycznie — nie fałszuj ich.
Częste pytania
- Jaka jest różnica między nagłówkiem żądania a odpowiedzi?
- Nagłówki żądania wysyła klient (przeglądarka lub aplikacja) do serwera; nagłówki odpowiedzi zwraca serwer wraz z odpowiedzią. Niektóre, jak Content-Type, występują po obu stronach.
- Czy nazwy nagłówków HTTP rozróżniają wielkość liter?
- Nie. Według specyfikacji nazwy nagłówków nie są case-sensitive, więc Content-Type i content-type to to samo. Title-Case to tylko konwencja.
- Do czego służy nagłówek Host?
- Host mówi serwerowi, którego domeny dotyczy żądanie. Jest obowiązkowy w HTTP/1.1 i pozwala jednemu serwerowi hostować wiele witryn według nazwy.
- Jak działa cache z nagłówkami?
- Cache-Control określa, jak długo odpowiedź może być cache'owana; ETag i If-None-Match pozwalają klientowi zapytać 'czy się zmieniło?' i otrzymać lekki 304 Not Modified.