I need to do a request to a server. I'm using Spring Boot 2.5 and restTemplate. The endpoint consumes the MediaType application/x-www-form-urlencoded, but when request is send returns a error "endpoint only accepts application/x-www-form-urlencoded for POST requests". In debug I saw that content type send in restTemplate is: application/x-www-form-urlencoded;charset=utf-8.
Code:
public void getToken(){
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<TokenResponseDTO> responses = restTemplate
.postForEntity(url, new HttpEntity<>(getForm(), getHeaders()), TokenResponseDTO.class);
log.info(responses.toString());
}
@NotNull
private MultiValueMap<String, String> getForm() {
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("param1", "1");
map.add("param2", "2");
map.add("param3", "3");
return map;
}
public HttpHeaders getHeaders(){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("header1", "aaa");
return headers;
}