자주 쓰는 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. |
참고
- 헤더 이름은 대소문자를 구분하지 않습니다. 관례적으로 타이틀 케이스(예: Content-Type)로 씁니다.
- 요청 헤더는 클라이언트를, 응답 헤더는 서버의 답변을 설명합니다.
- 브라우저는 Host나 Referer 같은 헤더를 자동으로 설정하므로 조작해서는 안 됩니다.
자주 묻는 질문
- 요청 헤더와 응답 헤더의 차이는?
- 요청 헤더는 클라이언트(브라우저나 앱)가 서버로 보내고, 응답 헤더는 서버가 답변과 함께 돌려줍니다. Content-Type처럼 양쪽에 다 나오는 헤더도 있습니다.
- HTTP 헤더 이름은 대소문자를 구분하나요?
- 아니요. HTTP 명세상 헤더 이름은 대소문자를 구분하지 않습니다. Content-Type과 content-type은 같습니다. Title-Case는 관습일 뿐입니다.
- Host 헤더는 무엇을 하나요?
- Host는 요청이 어느 도메인을 향하는지 서버에 알립니다. HTTP/1.1에서는 필수이며, 한 서버가 여러 사이트를 이름으로 구분하도록 합니다.
- 헤더로 캐시는 어떻게 동작하나요?
- Cache-Control은 응답을 얼마나 캐시할지 정하고, ETag와 If-None-Match로 클라이언트가 '바뀌었나?'라고 묻고 변함없으면 가벼운 304 Not Modified를 받습니다.