0

I'm experiencing some troubles with a simple matter.

I'm trying to send a request to other REST service

//getting restTemplate from RestTemplateBuilder.build()
//endpoint and rest of variables came in properties

Map<String, String> map = new HashMap<>();
map.put("app", app);
map.put("username", username);
map.put("password", password);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

String token = restTemplate.postForObject(loginEndpoint, headers, String.class, map);

And I recive:

Unexpected error occurred in scheduled task.

org.springframework.web.client.HttpClientErrorException: 400 Bad Request

The weird thing, when I use a simple CURL call and works smooth.

Already checked the variables and endpoint, and it's correct.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

1 Answers1

0

In this case, endpoint must have appropiate placeholders on end point url.

I made this method to do it easy:

private String placeHolders(Map<String, String> values){
    String response = "?";
    boolean first = true;
    for(Map.Entry<String, String> entry:values.entrySet()){
        if(first){
            first = false;
        }else{
            response+="&";
        }
        response+=entry.getKey()+"="+entry.getValue();
    }
    return response;
}

And the call now Is:

String token = restTemplate.postForObject(loginEndpoint+placeHolders, headers, String.class, map);