JAVA/Spring

HTTP Content-Type

워니- 2022. 3. 10. 23:31

1. <form> tag 

Method : Only POST

Content-Type : application/x-www-form-urlencoded

HTTP Message Body를 사용하지만 형식은 Query Parameter로 전달, ex) username=hello&age=20

※ form 데이터도 메세지 바디를 통해 읽을 수 있지만, request.getParameter를 통해 읽는 것이 편리하고 주로 사용

 

2. Message Body

Method : POST, PUT, PATCH

JSON, XML, Text(Raw) 형식

※ GET은 URL Query Parameter 형식으로, HTTP Message Body를 사용하지 않아서 Content-Type이 없다.

1) Text

메세지 바디의 바이트코드 획득 : request.getInputStream()

바이트코드 -> String 변환 : SteramUtils.copyToString(UTF-8)

2) JSON 

Content-Type : application/json

메세지 바디의 바이트코드 획득 : request.getInputStream()

바이트코드 -> String 변환 : SteramUtils.copyToString(UTF-8)

(String) JSON -> 객체 변환 : Jackson 라이브러리(ObjectMapper) 사용, obejectMapper.readValue(객체.class)

※ JSON도 결국 String 형식임