Saving and restoring cookies

You might have noticed that OB2 does not support domain-specific cookies, since most of the time it is required to work on a single domain with the same cookies.

But sometimes you might want to clear the cookie dictionary and perform some requests to some other website, and then put back the old cookies and continue working on the original website.

You can circumvent this by following this sample LoliCode

// This request will get a cookie with name 'cookieName' and value 'cookieValue'
BLOCK:HttpRequest
  url = "https://httpbin.org/cookies/set/cookieName/cookieValue"
  TYPE:STANDARD
  $""
  "application/x-www-form-urlencoded"
ENDBLOCK

// Now we transfer the cookies to a new variable called 'oldCookies'
var oldCookies = data.COOKIES.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
data.COOKIES.Clear();

// The cookies WILL NOT be sent in this request
BLOCK:HttpRequest
  url = "https://example.com"
  TYPE:STANDARD
  $""
  "application/x-www-form-urlencoded"
ENDBLOCK

// We restore the previous cookies
data.COOKIES = oldCookies;

// The cookies WILL be sent in this request
BLOCK:HttpRequest
  url = "https://example.com"
  TYPE:STANDARD
  $""
  "application/x-www-form-urlencoded"
ENDBLOCK
3 Likes