[RestTemplate] x-www-form-urlencoded

RestTemplate 으로 POST 전송시에 파라미터(application/x-www-form-urlencoded)로 메시지 보내기

 

Map으로 httpbody를 구성할 경우 아래와 같은 에러가 발생한다.

No HttpMessageConverter for java.util.HashMap and content type "application/x-www-form-urlencoded"

HashMap은 urlencoded에는 사용할 수 없다는 모양이다 왜죠...

이 경우 Multivaluemap을 사용하여 바디를 구성할 경우 전송이 된다.
웃긴게 값을 put을 하면 에러가 나고 add를 해야한다... 왜죠...

        // 2. make http body
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("grant_type", "authorization_code");
        body.add("client_id", KAKAO_API_KEY);
        body.add("redirect_uri", "http://localhost:8080/login");
        body.add("code", authCode);

        // 3. make http entity
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(body, headers);

        // 4. send request
        String response = restTemplate.postForObject(
                "https://kauth.kakao.com/oauth/token"
                , requestEntity
                , String.class);

 

 

 

https://sejoung.github.io/2019/12/2019-12-10-RestTemplet_POST_form/#RestTemplate-%EC%9C%BC%EB%A1%9C-POST-%EC%A0%84%EC%86%A1%EC%8B%9C%EC%97%90-%ED%8C%8C%EB%9D%BC%EB%AF%B8%ED%84%B0-application-x2F-x-www-form-urlencoded-%EB%A1%9C-%EB%A9%94%EC%8B%9C%EC%A7%80-%EB%B3%B4%EB%82%B4%EA%B8%B0

 

디버깅 참고 블로그

https://maivve.tistory.com/335

'┝ 개발 언어 > ┎ JAVA' 카테고리의 다른 글

[java] synchronized  (0) 2025.02.12
[java] multipart 파일 받을 때 주의할 점  (0) 2023.11.14
empty String과 null을 Integer value of할 경우  (0) 2023.11.08
getBytes  (0) 2023.05.17
생성자  (0) 2023.05.17