ContentType์
x-www-form-urlencoded๋ก
๋ช ์ํ๋๋,
HttpEntity์ ๋ด์ ๋ณด๋๋ค.
๋ฌด์จ ๊ด๊ณ๊ฐ ์๋ ๊ฑธ๊น?
HttpEntity
- ํค๋์ ๋ฐ๋๋ก ์ด๋ค์ง HTTP request, response ์ํฐํฐ๋ฅผ ๋ํ๋ธ๋ค.
- ์ฝ๊ฒ ์ค๋ช ํ์๋ฉด, HttpEntity๋ Http ์์ฒญ/์๋ต์ ํค๋+๋ฐ๋๋ฅผ ๋ฌถ๋ ์์์ ๊ฐ์์ ํค๋์ ๋ฐ๋๋ฅผ ํจ๊ป ๋ณด๋ด์ผ ํ ๋ ์ฌ์ฉํ๋ค.
- ํ์ ํ์ ์ผ๋ก๋ RequestEntity<T>(์์ฒญ์ ์ฉ, HTTP ๋ฉ์๋/URL๊น์ง ํฌํจ)์ ResponseEntity<T>(์๋ต์ ์ฉ, ์ํ์ฝ๋๊น์ง ํฌํจ)๊ฐ ์๋ค.
- HttpEntity๋ ์ํ ์ฝ๋๊ฐ ์์ผ๋ HttpStatus์ ์ํ ์ฝ๋๋ฅผ ๋ํ ์ ์๋ค.
- HttpEntity๋ ์์ฒญ/์๋ต ์์ชฝ์์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
x-www-form-urlencoded
- html form์ ํตํ POST ์ ์ก ๋ฐฉ์ ์ค ๊ฐ์ฅ ๊ธฐ๋ณธ์ด ๋๋ ContentType์ด๋ค.
- ๋ณด๋ด๋ ๋ฐ์ดํฐ๋ฅผ url ์ธ์ฝ๋ฉ ํ ์น ์๋ฒ์ ๋ณด๋ด๋ ๋ฐฉ์์ด๋ค.
- ๋ชจ๋ ๋ธ๋ผ์ฐ์ ๋ application/x-www-form-urlencoded content-type์ ๋ํด body์ ๋ฐ์ดํฐ๋ฅผ ์๋์ผ๋ก encoding ํ๋๋ก ๊ตฌํ๋์ด์๋ค.
- ๋ง์ฝ ์ ํ๋ฆฌ์ผ์ด์ ๋ก์ง์์ ํด๋นํ์ ์ผ๋ก POST ์์ฒญ์ ๋ณด๋ผ ๋๋ body์ ๋ํ ์ธ์ฝ๋ฉ์ด ๋๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํด์ผ ํ๋ค.
- ์ธ์ฝ๋ฉ์ด ํ์ํ๊ธฐ ๋๋ฌธ์ ํฌ๊ธฐ๊ฐ ํฐ ๋ฐ์ดํฐ์ ์ ํฉํ์ง ์๋ค.
- x-www-form-urlencoded์ ์ธ์ฝ๋ฉ ๊ท์น์ key=value ํ์์ด๋ฉฐ, ์ฌ๋ฌ ๊ฐ์ key-value ์์ '&'๋ก ๊ตฌ๋ถ๋๋ฉฐ ์ฌ๋ฌ ๋ฐ์ดํฐ๋ฅผ ํ ๋ฒ์ ์ ์ก ๊ฐ๋ฅํ๋ค.
- ๋ฐ์ดํฐ์ ํน์ ๋ฌธ์๋ ๊ณต๋ฐฑ๊ณผ ๊ฐ์ ๋ถ๋ถ์ด url ์ธ์ฝ๋ฉ ๊ท์น[RFC1738]์ ๋ฐ๋ผ ์ธ์ฝ๋ฉ ๋๋ค.
x-www-form-urlencoded์ HttpEntity ์ฝ๋ ์์
HttpHeaders tokenHeaders = new HttpHeaders();
tokenHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.add("grant_type", "authorization_code");
...
HttpEntity<MultiValueMap<String, String>> tokenReq = new HttpEntity<>(form, tokenHeaders);
์์ ์ฝ๋๋ ์นด์นด์ค ๊ฐํธ ๋ก๊ทธ์ธ์ ํ๋ฉด์ ์์ฑํ ์ฝ๋ ์ผ๋ถ์ด๋ค.
setContentType(MediaType.APPLICATION_FORM_URLENCODED);๋ฅผ ์ ์ด, ์ฌ๊ธฐ์ Content-Type์ x-www-form-urlencoded๋ก ๋ช ์ํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ง์ง๋ง ์ค์ ๋ณด๋ฉด HttpEntity๋ก ๊ฐ์ผ ๊ฑธ ํ์ธํ ์ ์๋ค.
๊ทธ๋ฌ๋ฉด ์ HttpEntity๊ฐ ์์ผ ํ๋ ๊ฑธ๊น?
application/x-www-form-urlencoded๋ ํผ ํ์์ด๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง๊ณ ์๋ค. ์ด ํผ ํ์์ ๋ฐ๋ ์์ ๋ค์ด์๋ ๋ฐ์ดํฐ๊ฐ ํค=๊ฐ&ํค=๊ฐ...์ผ๋ก URL ์ธ์ฝ๋ฉ ๋์ด ์๋ค๋ ๋ป์ด๋ค. ๊ทธ๋ฆฌ๊ณ ์๋ฒ์๊ฒ "๋ฐ๋๊ฐ ์ด๋ ๊ฒ ์๊ฒผ์ด์"๋ผ๋ ์ฌ์ค์ ์๋ ค์ผ ํ๋๋ฐ, ์๋ฆฌ๋ ๋ฐฉ๋ฒ์ด ํค๋์ ContentType์ "์ด ๋ฐ๋๋ ํผ ํ์์ ๋๋ค"๋ผ๊ณ ์ ์ด์ค์ผ ํ๋ค. ๊ทธ๋์ x-www-form-urlencoded๋ ํค๋์ ๋ฐ๋๋ฅผ ๋ ๋ค ๋ณด๋ด๋ ๊ฒ ์ค์ํ ๊ฒ์ด๋ค. (ํค๋๋ก ํ์์ ์๋ฆฌ๊ณ , ๋ฐ๋์ ๋ฐ์ดํฐ๋ ๋ณด๋ด์ผ ํ๋๊น!)
์์์ ๋งํ๋ฏ์ด HttpEntity๋ ๋ฐ๋์ ContentType ํค๋๋ฅผ ํ๋์ ์ธํธ์ฒ๋ผ ๋ฌถ์ด์ ๋ณด๋ธ๋ค.(๋ณดํต ํผ ์ ์ก์ฒ๋ผ ํค๋์ ๋ฐ๋๋ฅผ ํจ๊ป ๋ด๋ ๊ฒ ์ผ๋ฐ์ ์ด๊ธด ํ๋, ํค๋๋ ๋ฐ๋๋ง ๋ด์๋ ๊ฐ๋ฅํ๋ค) ๊ทธ๋์ HttpEntity๋ฅผ ์ฌ์ฉํ์ฌ ๊น๋ํ๊ฒ ๋ณด๋ด๊ธฐ ์ํจ๋ ์๊ณ , ํค๋์ ๋ฐ๋๋ฅผ ๊ฐ์ด ๋ณด๋ด๊ธฐ ์ํจ๋ ์๋ค.
+) MultiValueMap๋ ๋น์ทํ ์ด์ ๋ก x-www-form-urlencoded์ ํจ๊ป ์ฐ์ธ๋ค. ์๊น ๋ฐ๋ ์์ ๋ค์ด์๋ ๋ฐ์ดํฐ๋ ํค=๊ฐ&ํค=๊ฐ... ์ด๋ฐ ํํ๋ผ๊ณ ํ๋ค. ๊ฐ์ ํค์ ๋ค์ค ๊ฐ์ด ๊ฐ๋ฅํ ํํ์ธ๋ฐ, ์ด๋ฐ ํํ์๋ MutiValueMap์ด ๋ฑ ๋ง๋ค. ์๋ํ๋ฉด ์คํ๋ง์ FormHttpMessageConverter๊ฐ MultiValueMap<String,Stirng>์ ์ ํํ ํค=๊ฐ&ํค=๊ฐ... ํ์์ผ๋ก URL ์ธ์ฝ๋ฉํด ์ง๋ ฌํํ๊ธฐ ๋๋ฌธ์ด๋ค.
์๋์ ์ถ์ฒ๋ฅผ ๋จ๊ฒจ๋๊ฒ ์ต๋๋ค.
๋ธ๋ก๊ทธ์ ์ ๊ฐ ์ต๋ํ ์ดํดํ ๋ด์ฉ๊น์ง๋ง ์ ๋๊ฑฐ๋ผ
์ถ์ฒ์ ๋ค์ด๊ฐ์๋ฉด ๋ ๋ง์ ๋ด์ฉ์ด ์์ด ๊ณต๋ถํ์๋๋ฐ ๋์๋์ค๊ฒ๋๋ค.
๐ชฝ
ํ๋ฆฐ ๋ด์ฉ์ด ์๋ค๋ฉด ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์
[Spring Boot] HttpEntiy, ResponseEntity ๋?
[Spring Boot] HttpEntity, ResponseEntity ๋? ๊ณต๋ถ๋ฅผ ํ๋ ์ค ResponseEntity ๋ฅผ ์ฌ์ฉํ๊ฒ ๋์๋ค. ๊ทธ๋ฐ๋ฐ Controller์์ View์ ์ ๋ณด๋ฅผ ์ ์กํ ๋ Http status, header๋ฅผ ์กฐ์ ํ ์ ์๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค๊ณ ๋ง ์๊ณ ์ฌ
myeongdev.tistory.com
x-www-form-urlencoded ํ์ ์ด๋ (multipart/form-data์์ ์ฐจ์ด์ )
x-www-form-urlencoded์ multipart/form-data ์ฐจ์ด์ //๊ธฐ์กด์ ์ค์ ๋ springdoc consumes default-consumes-media-type: multipart/form-data //๋ณ๊ฒฝ๋ ์ค์ default-consumes-media-type: application/x-www-form-urlencoded ํ๋ก์ ํธ ์์ ์ค ๋
wildeveloperetrain.tistory.com
[๋คํธ์ํฌ] application/x-www-form-urlencoded, application/json ๋ฌด์จ ์ฐจ์ด๊ฐ ์๋๊ฑธ๊น?
PHP ๋ก ๋์ด์๋ ์ฝ๋๋ฅผ JAVA ๋ก ์ด๊ดํ๋ฉด์, Content-Type ์ด application/x-www-form-urlencoded, application/json ๋๋ค ์์ฒญ๋ฐ์ ์ ์๋ ์ฝ๋์์ application/json ๋ง ๋ฐ์ ์ ์๋๋ก ๋ณ๊ฒฝ๋ ์ผ์ด์ค
velog.io
'๐ท Spring > ๊ฐ๋ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Spring] PostMapping, PutMapping, PatchMapping์ ํน์ง๊ณผ ์ฐจ์ด์ (๋ฉฑ๋ฑ์ฑ) (0) | 2026.01.17 |
|---|---|
| [Spring] StringUtils.hasText()๋ก ๋ฌธ์์ด ์ ํจ์ฑ ๊ฒ์ฆํ๊ธฐ. (0) | 2025.09.16 |
| [Spring][JPA] @Entity๋ ๋ฌด์์ผ๊น? (0) | 2025.09.09 |
| [Spring] @Id์ @GeneratedValue์ด๋? (0) | 2025.09.08 |
| [SpringSecurity] @EnableWebSecurity, SecurityFilterChain ๊ฐ๋ ๊ณผ ์์ (2) | 2025.08.28 |