Search code examples
rubycookiesmechanize

How to manually add a cookie to Mechanize state?


I'm working in Ruby, but my question is valid for other languages as well.

I have a Mechanize-driven application. The server I'm talking to sets a cookie using JavaScript (rather than standard set-cookie), so Mechanize doesn't catch the cookie. I need to pass that cookie back on the next GET request.

The good news is that I already know the value of the cookie, but I don't know how to tell Mechanize to include it in my next GET request.


Solution

  • These answers are old, so to bring this up to date, these days it looks more like this:

    cookie = Mechanize::Cookie.new :domain => '.mydomain.com', :name => name, :value => value, :path => '/', :expires => (Date.today + 1).to_s
    agent.cookie_jar << cookie