Rest Template POST returning Bad Request

I am trying to get an auth token from a server using RestTemplate in Spring. However, I am getting a 400 bad Request. I understand that this is an issue in the parameters that i am sending in my request. I am able to successfully get the token using postman:
enter image description here

Content-Type is application/json

This is the code I am using for the request:

RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        OauthRequestUtil oauthRequest = new OauthRequestUtil();
        oauthRequest.setClientId(Constants.CLIENT_ID);
        oauthRequest.setClientSecret(Constants.CLIENT_SECRET);
        oauthRequest.setGrant_type(Constants.GRANTTYPE);
        oauthRequest.setResource(Constants.RESOURCE);

            HttpEntity<OauthRequestUtil> request = new HttpEntity<>(oauthRequest, headers);
            ResponseEntity<String> response = restTemplate.exchange(Constants.OAUTH_URL, HttpMethod.POST, request, String.class);

I am getting the 400 error on the restTempalte.exchange() line. The param values are correct, they are lifted straight from postman.

OauthRequestUtil is just a class with the private variables and getters/setters

public class OauthRequestUtil {

    private String clientId;
    private String clientSecret;
    private String grant_type;
    private String resource;

Leave a Reply

avatar
  Subscribe  
Notify of