• HTTP 메시지(요청과 응답 모두)에 담겨 보내는 데이터의 형식을 알려주는 헤더
  • Content-Type 헤더는 리소스의 media type을 나타내기 위해 사용
  • HTTP 표준 스펙을 따르는 브라우저와 웹서버는 Content-Type 헤더를 기준으로 HTTP 메시지에 담긴 데이터를 분석과 파싱
  • 중요한점은 HTTP 요청의 경우 GET방식인 경우에는 무조건 URL 끝에 쿼리스트링으로 value=text 형식으로 보내지기 때문에 Content-Type은 필요 X → GET방식으로 데이터를 전송 시 웹서버 입장에서는 value=text 형식 데이터라고 판단
  • Content-Type은 POST나 PUT처럼 메시지 BODY에 데이터를 보낼때 필요
  • 브라우저를 기준으로 설명하자면 AJAX를 통해 json 형식의 데이터를 전송하는 경우 Content-Type 값을 application/json 으로 지정하여 전송
  • 브라우저들은 어떤 경우에는 MIME 스니핑을 해서 Content-Type 헤더의 값을 꼭 따르지는 않음 → 해당 현상을 막기 위해, X-Content-Type-Options (en-US) 헤더를 nosniff으로 설정 가능


문법

Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something



중요한 Content-Type

1. application/octet-stream

  • MIME의 개별 타입 중 application에 속하는 타입으로, 8비트 단위의 binary data 의미
  • 특별히 표현할 수 있는 프로그램이 존재하지 않는 데이터의 경우 기본값으로 octet-stream을 사용 → 브라우저가 보통 자동으로 실행하지 않거나 실행할지 묻기도 하는 타입
  • Content-Disposition 헤더를 attachment로 줌으로써 해당 데이터를 수신받은 브라우저가 파일을 저장 또는 다른이름으로 저장 여부를 설정 가능

※ Content-Disposition 헤더

  • Disposition이란 기질, 성향, 배치, 배열 이란 뜻
  • HTTP Response Header에 들어가는 Content-Disposition은 HTTP Response Body에 오는 컨텐츠의 기질/성향을 알려주는 속성
  • default 값은 inline으로 web에 전달되는 data라고 생각하면 됨
  • 특수한 경우는 Content-Disposition에 attachment를 주는 경우로, 이때 filename과 함께 주게 되면 Body에 오는 값을 다운로드 받으라는 의미
    Content-Disposition: attachment; filename="hello.jpg"


2. text/plain

  • 텍스트 파일에 대한 기본값
  • 실제로 알려지지 않은 텍스트 파일일지라도 브라우저들은 디스플레이할 수 있다고 가정함
  • text/plain이 모든 종류의 텍스트 데이터를 의미하지는 않음
  • CSS 파일을 선언한 alignment로부터 text/plain 파일을 다운로드할 경우, text/plain으로 표현된다면 브라우저는 유효한 CSS 파일로 감지 X → CSS의 MIME 타입인 text/css이 사용해야함


3. text/css

  • 웹 페이지 내에서 보통 인터프리트되어야 하는 모든 CSS 파일들은 text/css 파일이 되어야 함.
  • .css 접미사를 가진 파일들을 CSS 파일이라고 인식하지 못해 text/plain 혹은 application/octet-stream MIME 타입으로 전송 → 해당 사항은 대부분의 브라우저들이 CSS 파일이라고 인식하지 못 하며 실행 X
  • 올바른 타입으로 CSS 파일을 서브하는데 특별한 주의가 필요


4. text/html

  • Content-Type 헤더의 값을 직접 설정하지 않으면, HTML 문서의 MIME 타입인 text/html로 자동 설정


Content-Type의 종류

1, Multipart Related MIME 타입

  • Content-Type: Multipart/related → 기본 형태
  • Content-Type: Application/X-FixedRecord


2. XML Media의 타입

  • Content-Type: text/xml
  • Content-Type: Application/xml
  • Content-Type: Application/xml-external-parsed-entity
  • Content-Type: Application/xml-dtd
  • Content-Type: Application/mathtml+xml
  • Content-Type: Application/xslt+xml


3. Application의 타입

  • Content-Type: Application/EDI-X12 → Defined in RFC 1767
  • Content-Type: Application/EDIFACT → Defined in RFC 1767
  • Content-Type: Application/javascript → Defined in RFC 4329
  • Content-Type: Application/octet-stream → 디폴트 미디어 타입은 운영체제 종종 실행파일, 다운로드를 의미
  • Content-Type: Application/ogg → Defined in RFC 3534
  • Content-Type: Application/x-shockwave-flash → Adobe Flash files
  • Content-Type: Application/json → JavaScript Object Notation JSON; Defined in RFC 4627
  • Content-Type: Application/x-www-form-urlencode → HTML Form 형태
  • Content-Type: application/xml
  • Content-Type: application/pdf

※ 참고

  • x-www-form-urlencode와 multipart/form-data은 둘다 폼 형태
  • x-www-form-urlencode은 대용량 바이너리 테이터를 전송하기에 비능률적
  • 대부분 첨부파일은 multipart/form-data를 사용


4. 오디오 타입

  • Content-Type: audio/mpeg → MP3 or other MPEG audio
  • Content-Type: audio/x-ms-wma → Windows Media Audio;
  • Content-Type: audio/vnd.rn-realaudio → RealAudio; 등등


5. Multipart 타입

  • Content-Type: multipart/mixed → MIME E-mail
  • Content-Type: multipart/alternative → MIME E-mail
  • Content-Type: multipart/related → MIME E-mail → Defined in RFC 2387 and used by MHTML(HTML mail)
  • Content-Type: multipart/formed-data → 파일 첨부


6. TEXT 타입

  • Content-Type: text/css
  • Content-Type: text/html
  • Content-Type: text/javascript
  • Content-Type: text/plain
  • Content-Type: text/xml


7. file 타입

  • Content-Type: application/msword → doc
  • Content-Type: application/pdf → pdf
  • Content-Type: application/vnd.ms-excel → xls
  • Content-Type: application/x-javascript → js
  • Content-Type: application/zip → zip
  • Content-Type: image/jpeg → jpeg, jpg, jpe
  • Content-Type: text/css → css
  • Content-Type: text/html → html, htm
  • Content-Type: text/plain → txt
  • Content-Type: text/xml → xml
  • Content-Type: text/xsl → xsl


8. image 타입

  • Content-Type: image/gif → gif 이미지
  • Content-Type: image/png → PNG 이미지
  • Content-Type: image/jpeg → JPEG 이미지
  • Content-Type: image/bmp
  • Content-Type: image/webp
  • Content-Type: image/svg+xml → SVG 이미지 (벡터 이미지)


9. video 타입

  • Content-Type: video/webm
  • Content-Type: video/ogg

'HTTP > HTTP 헤더' 카테고리의 다른 글

HTTP 프로토콜 ETag 헤더  (0) 2022.06.25
HTTP 프로토콜 Date 헤더  (0) 2022.06.25
HTTP 프로토콜 Content-Encoding 헤더  (0) 2022.06.25
HTTP 프로토콜 Connection 헤더  (0) 2022.06.25
HTTP 프로토콜 Accept-Ranges 헤더  (0) 2022.06.25

+ Recent posts