Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestTemplate and Cookie

I need to send an HTTP cookie, I'm using RestTemplate:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", "SERVERID=c52");
HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
ResponseEntity responses = restTemplate.exchange(webService.getValidateUserUrl(),
       HttpMethod.POST, requestEntity,  String.class, mapValidateUser);

However, the receiving server doesn't see the cookie.

like image 311
Ilkar Avatar asked Apr 16 '12 14:04

Ilkar


People also ask

How do you add 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");

CAN REST API set cookies?

A RESTful API may send cookies just like a regular Web Application that serves HTML. Cookies don't always violate the REST pattern. For example, the server might want to have its client remember a certain state, so that it can provide this state when requesting another resource at a later point.

Why RestTemplate is deprecated?

RestTemplate provides a synchronous way of consuming Rest services, which means it will block the thread until it receives a response. RestTemplate is deprecated since Spring 5 which means it's not really that future proof.

What is the replacement for RestTemplate?

WebClient offers a modern alternative to the RestTemplate with efficient support for both sync and async, as well as streaming scenarios. The RestTemplate will be deprecated in a future version and will not have major new features added going forward. We are writing a new project using spring boot 2.0.


2 Answers

The default rest template does not use a persistent connetion, here is what I use.

public class StatefullRestTemplate extends RestTemplate
{
    private final HttpClient httpClient;
    private final CookieStore cookieStore;
    private final HttpContext httpContext;
    private final StatefullHttpComponentsClientHttpRequestFactory statefullHttpComponentsClientHttpRequestFactory;

    public StatefullRestTemplate()
    {
        super();
        HttpParams params = new BasicHttpParams();
        HttpClientParams.setRedirecting(params, false);

        httpClient = new DefaultHttpClient(params);
        cookieStore = new BasicCookieStore();
        httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, getCookieStore());
        statefullHttpComponentsClientHttpRequestFactory = new StatefullHttpComponentsClientHttpRequestFactory(httpClient, httpContext);
        super.setRequestFactory(statefullHttpComponentsClientHttpRequestFactory);
    }

    public HttpClient getHttpClient()
    {
        return httpClient;
    }

    public CookieStore getCookieStore()
    {
        return cookieStore;
    }

    public HttpContext getHttpContext()
    {
        return httpContext;
    }

    public StatefullHttpComponentsClientHttpRequestFactory getStatefulHttpClientRequestFactory()
    {
        return statefullHttpComponentsClientHttpRequestFactory;
    }

}


public class StatefullHttpComponentsClientHttpRequestFactory extends HttpComponentsClientHttpRequestFactory
{
    private final HttpContext httpContext;

    public StatefullHttpComponentsClientHttpRequestFactory(HttpClient httpClient, HttpContext httpContext)
    {
        super(httpClient);
        this.httpContext = httpContext;
    }

    @Override
    protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri)
    {
        return this.httpContext;
    }
}
like image 128
ams Avatar answered Oct 08 '22 09:10

ams


You can also extend the RestTemplate:

public class CookieRestTemplate extends RestTemplate {

  @Override
  protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
    ClientHttpRequest request = super.createRequest(url, method);

    request.getHeaders().add("Cookie", "SERVERID=c52");
    return request;
  }

}

like image 29
ticktock Avatar answered Oct 08 '22 10:10

ticktock