[ HttpClientErrorException] - Team City rest api using spring resttemplate
I need to use Team City rest api in spring rest template in order to get build status but the code snap giving the error , could you help
solve this error ?
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("username",username);
headers.set("password",passweord);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(headers);
System.out.println(">>>>>>>" + restTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody());
Error :
for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 401 null] with root cause
org.springframework.web.client.HttpClientErrorException: 401 null
Note : The same API is working Advance Rest Client and Postman
Thanks : Muthu Selvan SR
Please sign in to leave a comment.
Hi,
While I'm not exactly familiar with the sprint rest template, HttpHeaders seems to add the headers via the "set" method as is. Basic authentication doesn't have a "username" and "password", but you should use the basic authentication header. Some examples here: https://stackoverflow.com/questions/21920268/basic-authentication-for-rest-api-using-spring-resttemplate