Home » Java » Curl is working on server but the HTTP Rest Template not working

Curl is working on server but the HTTP Rest Template not working

Posted by: admin October 29, 2018 Leave a comment

Questions:

I have some URLs which returning response on the server when invoked through CURL. But the same URLs are returning “400 Bad Request” when invoked through application.
This is the class which I am using for HTTP Requests –

public class HttpUtilForUM {

private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

public static String restTemplate(String path, String json, HttpMethod method) {
    logger.info("HttpUtilForUM - URL - {}",  path);
    logger.info("HttpUtilForUM - Method - {}, Json - {}", method, json);
    HttpHeaders headers = new HttpHeaders();
    headers.add("auditID", MDC.get("auditID"));
    headers.set("Accept", "application/json");
    headers.setContentType(MediaType.APPLICATION_JSON);
    String result = "";
    ResponseEntity<String> response = null;
    try {
        HttpEntity<Object> request = new HttpEntity<Object>(json, headers);
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
        template.setRequestFactory(requestFactory);
        response = template.exchange(path, method, request, String.class);
        logger.info("Response from HttpUtilForUM - {}", response);
        HttpHeaders headers2 = response.getHeaders();
        Set<String> keySet = headers2.keySet();
        for (String string : keySet) {
            List<String> list = headers2.get(string);
            logger.debug("Headers key - {}", string);
            logger.debug("Headers value - {}", list);
        }
        result = response.getBody();
        logger.info("API Response - {}", result);
    }
    catch (HttpStatusCodeException hsec) {
        hsec.printStackTrace();
        result = handleHsecException(hsec);
    }
    catch (Exception e) {
        logger.error(e.getMessage(), e);
        result = "{\"status\":\"error\",\"data\":\"" + e.getMessage() + "\"}";
        result = e.getMessage();
    }
    return result;
}

This is the error which I am getting –

org.springframework.web.client.HttpClientErrorException: 400 Bad request
        at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
        at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:614)
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:570)
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530)
        at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:448)
        at com.t.g.c.service.utils.HttpUtilForUM.restTemplate(HttpUtilForUM.java:47)
        at com.t.g.c.service.utils.UM.getUserDetailsResponse(UserManagement.java:87)
        at com.t.g.c.s.custom.CustomUserDetailsService.loadUserByUsername(CustomUserDetailsService.java:78)
        at org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper.loadUserDetails(UserDetailsByNameServiceWrap
per.java:53)
        at org.springframework.security.cas.authentication.CasAuthenticationProvider.loadUserByAssertion(CasAuthenticationProvider.ja
va:214)
        at org.springframework.security.cas.authentication.CasAuthenticationProvider.authenticateNow(CasAuthenticationProvider.java:1
59)
        at org.springframework.security.cas.authentication.CasAuthenticationProvider.authenticate(CasAuthenticationProvider.java:142)
        at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:167)
        at org.springframework.security.cas.web.CasAuthenticationFilter.attemptAuthentication(CasAuthenticationFilter.java:269)
        at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProc
essingFilter.java:217)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        at org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:100)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIn
tegrationFilter.java:53)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)

I have tried setting the MediaType to All but didn’t worked.
Please help. Thanks in advance.

Answers: