Ian Vink
Ian Vink

Reputation: 68790

RestSharp 109: Adding cookies to every request

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?

Upvotes: 1

Views: 907

Answers (1)

Alexey Zimarev
Alexey Zimarev

Reputation: 19640

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)

Upvotes: 2

Related Questions