RestSharp, Cookie Management & Cookie Reuse

7,262 views
Skip to first unread message

Sathyajith Bhat

unread,
Nov 30, 2011, 1:43:22 PM11/30/11
to rest...@googlegroups.com
I realize this has been posted before (  https://groups.google.com/forum/#!topic/restsharp/2my8sz53_6g  ) but I haven't found a way get around my problem. 

I am using RestSharp 102.4. The API I'm trying to access is NewsBlur

The app flow is like this: 

 - Login to the app.

request.AddParameter("username", _username);
request.AddParameter("password", _password);
request.Resource = "api/login";
return Execute<LoginResponse>(request);

where Execute is the same block of code as present in the recommended usage wiki

 - Fetch the feeds

            var request = new RestRequest(Method.GET); 
            request.Resource = "reader/feeds";
            request.AddParameter("include_favicons", include_favicons);
            request.AddParameter("flat", flat);
            request.AddParameter("update_counts", update_counts); 
            return Execute<Feed>(request);

_cookie_name & _cookie_value are set during the Login phase. Now, during this phase, the cookies obtained from the login are not obtained and as a result trying to get the feeds results in 403. I tried setting Cookies obtained in the login phase using:

            request.AddHeader("Cookie: ",_cookie_name +"=" + _cookie_value);

but an error message was raised,

     ErrorMessage "Specified value has invalid HTTP Header characters.\r\nParameter name: name"

Could anyone let me know as to where I am going wrong? How can I set the cookies or make RestSharp use the existing ones?

Note: The issue here ( https://github.com/restsharp/RestSharp/pull/181 ) mentions reuse of CookieContainers for same RestClient, so I am not creating a new instance of RestClient. 


Sathyajith Bhat

unread,
Nov 30, 2011, 1:46:16 PM11/30/11
to rest...@googlegroups.com
Um, I should have mentioned I've had to change Execute a bit, since NewsBlur doesn't use BasicAuth. The Execute piece is like this:

            _client.BaseUrl = _apiURL;
            var response = _client.Execute<T>(request);
            // _cookie
            foreach (var c in response.Cookies)
            {
                _cookie_name = c.Name;
                _cookie_value = c.Value;
            }
            return response.Data;

Sathyajith Bhat

unread,
Dec 1, 2011, 12:54:28 PM12/1/11
to rest...@googlegroups.com
Figured out my problem:

1.     request.AddHeader("Cookie: ",_cookie_name +"=" + _cookie_value); is incorrect, went back to reading the docs which mention:

You can also add header and cookie paramters with AddParameter() 

So changed that to 

        request.AddParameter(_cookie_name, _cookie_value, ParameterType.Cookie);

And it seems to be working fine.

Thanks!

Devesh Tripathi

unread,
Jul 24, 2012, 10:56:42 AM7/24/12
to rest...@googlegroups.com
Here is my code to login into RightScale API. I get the response alright, but i am not able to save the cookie that i receive in the response.
I need to use this cookie for subsequent requests which is not happening. If you could help me here, i would really appreciate it. 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);
Reply all
Reply to author
Forward
0 new messages