Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestTemplate client with cookies

I'm writing a simple client in Java to allow reusable use of proprietary virus scanning software accessible through a RESTful API. To upload a file for scanning the API requires a POST for Connect, followed by a POST for Publishing the file to the server. In the response to the Connect POST there are cookies set by the server which need to be present in the subsequent POST for publishing the file. I'm currently using Spring RestTemplate in my client.

My question is how do I access the cookies in the response to forward back to the server with the subsequent POST? I can see that they are present in the header that is returned but there are no methods on the ResponseEntity to access them.

like image 526
Tom Avatar asked Apr 04 '14 04:04

Tom


People also ask

How do you send cookies to RestTemplate?

RestTemplate has a method in which you can define Interface ResponseExtractor<T> , this interface is used to obtain the headers of the response, once you have them you could send it back using HttpEntity and added again. . add("Cookie", "SERVERID=c52");

How do I add cookies to ResponseEntity?

Another way is to add the cookie as raw Set-Cookie header while building ResponseEntity object: HttpHeaders headers = new HttpHeaders(); headers. add("Set-Cookie","platform=mobile; Max-Age=604800; Path=/; Secure; HttpOnly"); ResponseEntity. status(HttpStatus.

CAN REST API set cookies?

No, REST does not allow cookies, because a) they are independent of application state; b) they have no defined semantics, cf.


2 Answers

RestTemplate has a method in which you can define Interface ResponseExtractor<T>, this interface is used to obtain the headers of the response, once you have them you could send it back using HttpEntity and added again.

 .add("Cookie", "SERVERID=c52");

Try something like this.

String cookieHeader = null;

new ResponseExtractor<T>(){
      T extractData(ClientHttpResponse response) {
        response.getHeaders();
      }
}

Then

  HttpHeaders headers = new HttpHeaders();
  headers.add("Cookie", cookieHeader );

  ResponseEntity<byte[]> response = restTemplate.exchange("http://example.com/file/123",
      GET,
      new HttpEntity<String>(headers),
      byte[].class);

Also read this post

like image 144
Koitoer Avatar answered Oct 07 '22 15:10

Koitoer


I've solved the problem by creating an interceptor which stores a cookie and puts it in next requests.

public class StatefulRestTemplateInterceptor implements ClientHttpRequestInterceptor {
    private String cookie;

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        if (cookie != null) {
            request.getHeaders().add(HttpHeaders.COOKIE, cookie);
        }
        ClientHttpResponse response = execution.execute(request, body);

        if (cookie == null) {
            cookie = response.getHeaders().getFirst(HttpHeaders.SET_COOKIE);
        }
        return response;
    }
}

Set the interceptor for your RestTemplate:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder templateBuilder) {
    return templateBuilder
            .requestFactory(new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory()))
            .interceptors(new StatefulRestTemplateInterceptor())
            .build();
}
like image 19
Ilya Lysenko Avatar answered Oct 07 '22 14:10

Ilya Lysenko