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

1

8

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?

Yonita answered 24/7, 2012 at 13:51 Comment(4)
That should be RESTful because it works for one user account meaning i can query using RightScale API for one user account. But if change only the username and password variables in the code, it doesnt allow me to go past the login thing. Probably i need to save cookies but i am not sure how to do that.Yonita
Sorry, if you need to save a session cookie, it's by definition not RESTful. But anyway, take a look at github.com/restsharp/RestSharp/wiki/CookiesLiechtenstein
How to use cookiecontainer in this scenario so that it gets saved and i can use it for subsequent requests please. var request = new RestRequest(); request.Resource = "login"; var client = new RestClient(); client.BaseUrl = _baseUrl; client.Authenticator = new HttpBasicAuthenticator(_username, _password); var myparm = new RestSharp.Parameter { Name = "X-API-VERSION", Value = "1.0", Type = ParameterType.HttpHeader }; request.AddParameter(myparm); var response = client.Execute(request);Yonita
I'm not deciphering all that. Edit it into the question, where it can be formatted appropriately.Liechtenstein
A
8

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

Apparition answered 30/10, 2015 at 3:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.