← 返回首页
Request: headers property - Web API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Request: headers property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2017년 3월.

참고 : 이 기능은 Web Worker에서 사용할 수 있습니다.

Request 인터페이스의 headers 읽기 전용 속성은 요청에 연관된 Headers 객체를 포함합니다.

In this article

Headers 객체입니다.

예제

아래 스니펫에서는 Request() 생성자를 활용하여 새 요청 (스크립트와 동일한 디렉토리에 위치한 이미지 파일) 을 생성하고, 요청 헤더를 변수에 저장합니다.

js
const myRequest = new Request("flowers.jpg"); const myHeaders = myRequest.headers; // Headers {}

Headers 객체에 헤더를 추가하기 위해 Headers.append 를 활용합니다. 그리고 두번째 init 매개변수를 활용하여 새 요청을 생성하고, init 옵션으로 헤더를 전달합니다.

js
const myHeaders = new Headers(); myHeaders.append("Content-Type", "image/jpeg"); const myInit = { method: "GET", headers: myHeaders, mode: "cors", cache: "default", }; const myRequest = new Request("flowers.jpg", myInit); const myContentType = myRequest.headers.get("Content-Type"); // 'image/jpeg' 를 반환합니다.

명세서

Specification
Fetch
# ref-for-dom-request-headers②

브라우저 호환성

Enable JavaScript to view this browser compatibility table.

같이 보기