Content-Type 헤더는 현재 리퀘스트 또는 리스폰스의 바디에 들어 있는 데이터가 어떤 타입인지를 나타낸다
Content-Type 헤더의 값은 '주 타입(main type)/서브 타입(sub type)'의 형식으로 나타낸다
위 타입들에 속하지 않는 것들은, 보통 application이라고 하는 주 타입에 속한다
텍스트 파일 이외의 파일들을 보통 바이너리 파일(binary file)이라고 하는데 이 바이너리 파일들 중에서도 특정 확장자(.png, .mp4 등)의 포맷에 해당하지 않는 데이터들을 보통 이렇게 application/octet-stream으로 나타낸다
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이 적혀있다.