Content-Type

웅평·2023년 7월 13일
0

Content-Type 헤더

Content-Type 헤더는 현재 리퀘스트 또는 리스폰스의 바디에 들어 있는 데이터가 어떤 타입인지를 나타낸다

Content-Type 헤더의 값은 '주 타입(main type)/서브 타입(sub type)'의 형식으로 나타낸다

  1. 주 타입이 text인 경우(텍스트)
  • 일반 텍스트 : text/plain
  • CSS 코드 : text/css
  • HTML 코드 : text/html
  • JavaScript 코드 : text/javascript
  1. 주 타입이 image인 경우(이미지)
  • image/bmp : bmp 이미지
  • image/gif : gif 이미지
  • image/png : png 이미지
  1. 주 타입이 audio인 경우(오디오)
  • audio/mp4 : mp4 오디오
  • audio/ogg : ogg 오디오
  1. 주 타입이 video인 경우(비디오)
  • video/mp4 : mp4 비디오
  • video/H264 : H264 비디오

위 타입들에 속하지 않는 것들은, 보통 application이라고 하는 주 타입에 속한다

  • application/json : JSON 데이터
  • application/octet-stream : 확인되지 않은 바이너리 파일

텍스트 파일 이외의 파일들을 보통 바이너리 파일(binary file)이라고 하는데 이 바이너리 파일들 중에서도 특정 확장자(.png, .mp4 등)의 포맷에 해당하지 않는 데이터들을 보통 이렇게 application/octet-stream으로 나타낸다

Content-Type 헤더가 존재하면, 바디의 데이터를 직접 확인해서 그 타입을 추론하지 않아도 되기 때문에 필요하다

Content-Type 설정

const newMember = {
  name: 'somsom',
  email: 'somsom@googol.com',
};

fetch('https://learn.codeit.kr/api/members', {
  method: 'POST',
  headers: { 
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(newMember),
})
  .then((response) => response.text())
  .then((result) => { console.log(result); });

개발자 도구를 확인해 보면 Content-Type에 application/json이 적혀있다.

0개의 댓글