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);
디버깅 참고 블로그
'┝ 개발 언어 > ┎ 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 |