Spring RestTemplate With Cookie In Header

By | August 25, 2018

Spring rest template hit endpoint with cookie in header can be like this:

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.ALL));
headers.add("Cookie", "ID=SPRING-JAVA");
HttpEntity entity = new HttpEntity<>(headers);

ResponseEntity response = restTemplate.exchange(
    "https://www.cimblo.com", HttpMethod.GET, entity, String.class);

Spring RestTemplate With Cookie In Header

Spring RestTemplate With Cookie In Header

In this example i’m using java 8 and spring boot version 1.5.8 with starter-web dependency.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

One thought on “Spring RestTemplate With Cookie In Header

  1. Pingback: How To Handle Spring Data Pageable in Feign Client ~ Cimblo.com

Leave a Reply

Your email address will not be published. Required fields are marked *