RestSharp 109: Adding cookies to every request

522 views Asked by At

In RestSharp 108 and below the RestClient could add common cookies to every call like this:

RestClient.CookieContainer.Add(new Cookie("hello", "hello", "/", _baseUrl.Host));

This is no longer the case in RestSharp 109. How can we add a seriers of cookies to every Request?

1

There are 1 answers

0
Alexey Zimarev On

The client-level cookie container was removed because it was harmful in most of the use cases as it kept cookies between the requests, which might cause undesired leaks of private cookies.

You can still add a custom cookie container to the client by configuring the message handler:

options.ConfigureMessageHandler = 
    h => {
        var handler = (HttpClientHandler)h;
        handler.CookieContainer = myContainer;
        handler.UseCookies = true;
        return handler;
    }

The next RestSharp version will allow adding default cookies using the cookie container provided via options (https://github.com/restsharp/RestSharp/pull/2042)