Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save cookies from RestResponse and pass them to the next RestRequest?

If anyone could help me out with my issue, I would be really thankful.

I have written a C# code using RestSharp library to interact with RightScale API.

The code works fine with one set of username and password, but when I replace the username and password with a new one, I get the response "Basic auth is deprecated for calls other than login. Please 'login' to get a session and pass the session back for further actions."

Can anyone guide me in the right direction? I find it really weird that the code works for one set of credentials only and not with any other username and password.

How do I save cookies and pass them as reference in the next RestRequest?

like image 811
user1548923 Avatar asked Jul 24 '12 13:07

user1548923


1 Answers

RestSharp 102.4+ supports using a shared System.Net.CookieContainer for all requests from the same IRestClient. By doing so, any cookies set or unset in responses will be used in subsequent requests. In order to use a shared CookieContainer, simply set the property on your RestClient instance before using it:

var client = new RestClient("http://server/");
client.CookieContainer = new System.Net.CookieContainer();

Source: https://github.com/restsharp/RestSharp/wiki/Cookies

like image 142
Marcos Avatar answered Nov 15 '22 04:11

Marcos