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.
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");
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.
No, REST does not allow cookies, because a) they are independent of application state; b) they have no defined semantics, cf.
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
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With