Java REST Client POST with Cookie

In previous post I showed how to get cookie from server response with RestTemplate and exchange method.

In this article we will use that cookie in order to call other server API GetUsers.

private void postMethodWithCookie(String secret) {
	final String url = "";

	RestTemplate restTemplate = new RestTemplate();
	HttpHeaders requestHeaders = new HttpHeaders();
	requestHeaders.add("Cookie", "credentials=" + secret);

	HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
	ResponseEntity response =, HttpMethod.POST, requestEntity, Users.class);
	Users users = (Users) response.getBody();

As you can see from the code above, first we creating HttpHeader with our given secret key, then we adding it to the HttpEntity and finally calling HTTP exchange method. In case if the secret key is correct server will return the list of users in response.


  1. I have tried this ,but at the server side ,I cannot get the cookie info by request.getCookies(), I have to use request.getHeader(“Cookie”). and because of this ,I can not get the session info .plz tell me how to solve this ,thx.


Please enter your comment!
Please enter your name here