0

I'm using Spring's (version 2.5.4) RESTTemplate to make a POST call to a REST end point (url). Here is the code I have:

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String,String> requestBody = new LinkedMultiValueMap<>();
    requestBody.add("token", "abc");
    HttpEntity<MultiValueMap<String,String>> request = new HttpEntity<>(requestBody, headers);
    try
    {       
        ResponseEntity<TokenValidationResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, TokenValidationResponse.class);   
        TokenValidationResponse response = responseEntity.getBody();
        Boolean isActive = response.getActive();
        if(isActive == null || !isActive) {
            log.info("The token supplied isn't active");
        }
    }
    catch(RestClientException e)
    {
        log.error("An error occurred while posting query to security URI for token validation", e);
    }

I keep running into a 400 bad request -

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{"error_description":"token parameter is required for the security endpoint.","error":"invalid_request"}]
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:186)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:819)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:777)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:602)

This is the requirement given for the security endpoint url - This endpoint accepts only the HTTP POST method. The required Content-Type value is application/x-www-form-urlencoded. The endpoint has the following parameters: token

I can see the token being passed in though. I am able to verify that the call works fine on POSTMAN when I supply the token as part of the body. I was wondering if there is something I am missing?

linuxNoob
  • 600
  • 2
  • 14
  • 30

1 Answers1

0

You need to specify the token in the headers for example:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer "+accessToken);

I suggest to see this helpfull post

Med Elgarnaoui
  • 1,612
  • 1
  • 18
  • 35
  • This is the requirement given for the security endpoint url - This endpoint accepts only the HTTP POST method. The required Content-Type value is application/x-www-form-urlencoded. The endpoint has the following parameters: token. I don't think they want us to pass it as part of authorization header – linuxNoob Mar 09 '22 at 20:41
  • Tested it out, still the same error – linuxNoob Mar 09 '22 at 21:02
  • take a look at the post mentioned on the answer – Med Elgarnaoui Mar 09 '22 at 21:28