응답 본문을 내려받지 않고 상태 코드와 응답 헤더만 확인하는 레시피입니다. 캐시 정책·콘텐츠 타입·리다이렉트 점검에 유용합니다.
응답 본문을 내려받지 않고 상태 코드와 응답 헤더만 확인하는 레시피입니다. 캐시 정책·콘텐츠 타입·리다이렉트 점검에 유용합니다.
# -I sends HEAD and prints only headers:
curl -I https://example.com
# -i sends the full request and prints headers + body:
curl -i https://example.com
# -D - dumps headers to stdout while saving the body:
curl -D - -o page.html https://example.comconst res = await fetch('https://example.com');
console.log(res.status, res.statusText);
for (const [k, v] of res.headers) console.log(`${k}: ${v}`);
console.log(res.headers.get('content-type'));import requests
res = requests.head('https://example.com') # or .get()
print(res.status_code)
for k, v in res.headers.items():
print(f'{k}: {v}')curl의 `-I`(대문자)는 HEAD 요청을 보내 헤더만 받습니다. 본문까지 함께 보려면 `-i`(소문자)를, 본문은 파일로 저장하면서 헤더는 화면에 덤프하려면 `-D -`를 씁니다. HEAD를 지원하지 않는 서버에서는 `-i`로 GET을 쓰세요.
브라우저 `fetch`에서 CORS 제약에 주의하세요. 크로스 오리진 응답에서는 서버가 `Access-Control-Expose-Headers`로 명시하지 않은 헤더는 JS에서 읽을 수 없습니다. `Content-Length`·커스텀 헤더 등이 안 보이면 대개 이 때문입니다.
자주 보는 헤더로는 콘텐츠 형식·인코딩을 알려주는 `Content-Type`, 캐시 수명을 정하는 `Cache-Control`, 검증자 `ETag`, 리다이렉트 목적지 `Location`, 속도 제한 정보 `X-RateLimit-*` 등이 있습니다. 헤더 이름은 대소문자를 구분하지 않으므로 `get('content-type')`처럼 소문자로 조회해도 됩니다.