I have cookie String value returned by code below:

HttpResponse response = http.send(request); 
String cookieString = response.getHeader('Set-Cookie');

Now I am trying to construct a Pagereference object and set all above cookies to object by setCookies method. So I need to convert cookieString to List<Cookie> .Below is sample cookieString

 domain=domain.com; path=/; HttpOnly;_shovell_session2=;
 domain=domain.com; path=/; expires=Tue, 31-May-2016 07:21:29 GMT;;
 api_userid=2505; domain=domain.com; path=/; expires=Tue, 31-May-2016 07:21:29 GMT;;
 domain=domain.com; path=/; expires=Tue, 31-May-2016 07:21:29 GMT;;
 visits=1; path=/

Cookie object has constructor of below type

Cookie(name, value, path, maxAge, isSecure)

I can think of writing String splitting logic but that would be prone to exceptions. Is there any other better way of doing this?

  • Nope, you will have to apply String splitting logic. It works fine. – AtulRajguru9 May 27 '16 at 8:53

You cannot cast type String to type List<Cookie>. You have to split the cookie string by ;; and then add them to the list. If you have some problem, I can post sample code.

  • Yes I can write splitting logic. But In that case I am expecting cookie sting to be of same format all the time. I see possibilities of exceptions if some special character came in between or split logic didn't worked at some day. – Mukesh Verma May 27 '16 at 8:50
  • I don't think so. Your cookies is always separeted by ;; – DarkSkull May 27 '16 at 9:08
  • The OP already said that he can manually use the string splitting logic... – mkorman May 27 '16 at 9:48
  • please, vote my answer! – DarkSkull May 27 '16 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.