Setting multiple cookies for open-uri

Viewed 493

I am trying to figure out how I would go about using ['set-cookie'] to add multiple cookies to the request for the web page. If I had the cookies in an array as such:

cookie1 = {'domain': 'Website', 'name': 'xyz', 'value': 'ASH', 'path': '/', 'httpOnly': False, 'secure': False}

cookie2 = {'domain': 'Website', 'name': 'xyz', 'value': 'ASH', 'path': '/', 'httpOnly': False, 'secure': False}


page = Nokogiri::HTML(open("a webpage"), "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36", "Cookie" => cookie1['set-cookie'] cookie2['set-cookie']) 

I have been struggling to find any documentation on how this would integrate into open-uri.

1 Answers

You could join the cookies with semicolons:

cookies = cookie1,cookie2].map{|c| c.map{|k,v| "#{k}=#{v}"}.join('; ')}.join('; ')
open(..., "Cookie" => cookies)
Related