web developer

[java] Content-Type 본문

Language/Java

[java] Content-Type

trueman 2023. 1. 15. 21:31
728x90
728x90

Content-Type 정의


Content-Type 이란?

HTTP 통신에서 전송되는 데이터의 타입을 나타내는 header정보 중 하나이다.

Content-Type에 따라 데이터를 받는 측에서는 데이터를 어떻게 처리해야 할지를 판단한다. 여기서 데이터를 받는 측은 Request(브라우저) 또는 Response(웹서버) 둘다 포함 된다.


Request Headers

- 요청 헤더는 HTTP 요청에서 사용되지만 메시지의 컨텐츠와 관련이 없는 HTTP 헤더이다.
- 보통 Fetch될 리소스나 클라이언트 자체에 대한 정보를 포함하여 서버로 보내진다.

- 브라우저에서 웹서버로 이미지 데이터(일반텍스트)를 보낼경우 Request Header에 Content-Type을 지정해서 보낸다.

 

https://wonit.tistory.com/310?category=749910

 

[HTTP-Header] HTTP Request Header :: 요청 헤더

HTTP 요청 헤더는 서버로 요청할 데이터의 정보가 담겨있는 헤더이다. 보통 Fetch될 리소스나 클라이언트 자체에 대한 정보를 포함한다. 여기서 우리는 자주 사용하는 유명한 헤더에 대해서만 알

wonit.tistory.com


Response Headers

- 위치 또는 서버 자체에 대한 정보(이름, 버전)과 같이 응답에 대한 부가적인 정보를 갖는 헤더이다.

- 웹서버에서 브라우저로 데이터(이미지 파일)를 보낼경우 Response Header에 Content-Type은 image/svg를 지정해서 보낸다.

 

https://wonit.tistory.com/311?category=749910 

 

[HTTP-Header] HTTP Response Header :: 응답 헤더

HTTP 응답 헤더는 요청에 따라 적절한 로직이 수행되고 결과로 응답할 HTTP 메시지의 헤더를 말 한다. 여기서 우리는 자주 사용하는유명한 헤더에 대해서만 알아볼 것이다. 헤더는 크롬의 개발자

wonit.tistory.com

 

이렇게 데이터를 받는 측에서는 Content-Type를 확인 후 데이터를 어떻게 분석, 파싱할지 정하고 처리한다. Content-Type으로 요청 또는 응답의 데이터가 어떤 형식인지 판단할 수 있다.

 

Content-Type 특징


Content-Type Header가 없다면?

 

특정 data(img, viedo 등)를 Content-Type없이 보내면 data를 받는 쪽에서는 단순 텍스트 데이터로 받는다.


HTTP GET메소드에서 Content-Type은?

 

HTTP메소드에서 GET방식은 value=text 형식으로 보내지기 때문에 Content-Type은 필요없다.
HTTP메소드에 POST, PUT처럼 Body에 data를 보낼때 Content-Type이 필요하다.
즉, ajax를 사용해서 클라이언트가 서버에서 API 요청할 시에 Content-Type를 application/json으로 지정한다.


 

MIME 정의


Content-Type의 종류를 알기 전에 먼저 MIME에 대한 지식이 필요하다.

그 이유는 HTTP통신에서 Content-Type은 표준 MIME에 정의된 것을 사용한다.


MIME (Multipurpose Internet Mail Extensions) 은?

 

전자 우편을 위한 인터넷 표준 포맷이다. 전자우편은 7비트 ASCII 문자를 사용하여 전송되기 때문에, 8비트 이상의 코드를 사용하는 문자나 이진 파일들은 MIME 포맷으로 변환되어 SMTP로 전송된다. 실질적으로 SMTP로 전송되는 대부분의 전자 우편은 MIME 형식이다.
MIME 표준에 정의된 content types은 HTTP와 같은 통신 프로토콜에서 사용되며, 점차 그 중요성이 커지고 있다.


 

Content-Type / 문법


Content-Type 문법은?

 

/로 구분되며 type은 카테고리를 나타내며 subtype은 개별 혹은 멀티파트 타입을 나타낸다.
쉽게 생각하면 type은 주분류, subtype은 주분류에서 나눈 종류로 생각하면 된다.
또한, 스페이스는 허용되지 않고 대소문자를 구분하지 않지만 통상적으로 소문자로 쓰여진다.

 

예를 들어, 데이터가 이미지일 경우

 

Content-Type에 type은 imge가 된다.
imge에서도 png, jpg, gif 등 다양한 확장자가 있으며, subtype에 imge에 맞는 확장자를 나타낸다.
png 이미지를 전송한다면, Content-Type은 image/png가 된다.

 


 

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 형태

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

 

728x90
728x90
Comments