Search code examples
rubyrest-clienthttparty

How do I view the response cookies from a HTTParty get request?


I'm using httparty to do a GET request to a certain website. I need the response cookies in order to successfully make a POST request to login.

rest_client makes it really easy, all I have to do is:

get_request = RestClient.get('<REDACTED>')
response_cookies = get_request.cookies

# => 
{
 "sessiontype"=>"mpb",
 "aac"=>"741F9EC20A4C422369F7564445611591",
 "Expires"=>"Sun",
 "Path"=>"%2F",
 "Domain"=>"<REDACTED>",
 "internetbankierenmi"=>"1559079104.20480.0000",
 "TSdb640d"=>"d17ca2538ee2215b647c3466d4b06da7ec33c7a21dc7217953d3ffe7d4efbe89959deba9debace3f579e71c9e27e0b6b1ea2c663"
}

But I want to do the same with httparty. So my question is how do I view/access the response cookies from a GET request in httparty?


Solution

  • Since cookies are part of the response header, you can access them from the response header in httparty.

    require 'httparty'
    
    r = HTTParty.get('<REDACTED>')
    
    r.headers
    # => {"date"=>["Sat, 26 Jul 2014 19:34:09 GMT"], "cache-control"=>["no-cache", "no-store"], "pragma"=>["no-cache"], "expires"=>["Thu, 01 Jan 1970 00:00:00 GMT"], "content-length"=>["13987"], "set-cookie"=>["sessiontype=mpb; Secure", "aac=742A83A50A4C422C24D6F952C4BF6355; Expires=Sun, 26 Jul 2015 19:34:08 GMT; Path=/; Domain=<REDACTED>; Secure", "internetbankierenmi=1777182912.20480.0000; path=/", "TSdb640d=de34831032c17e8b66f123633372a9b341a9773368fccca553d402b1d4efbe89959deba9debace3fe0d47c86e27e0b6b1669bf05; Path=/"], "vary"=>["Accept-Encoding,User-Agent"], "content-type"=>["text/html;charset=ISO-8859-1"], "content-language"=>["en"], "connection"=>["close"], "strict-transport-security"=>["max-age=8640000"]}
    
    r.headers['set-cookie']
    # => "sessiontype=mpb; Secure, aac=742A83A50A4C422C24D6F952C4BF6355; Expires=Sun, 26 Jul 2015 19:34:08 GMT; Path=/; Domain=.ing.nl; Secure, internetbankierenmi=1777182912.20480.0000; path=/, TSdb640d=de34831032c17e8b66f123633372a9b341a9773368fccca553d402b1d4efbe89959deba9debace3fe0d47c86e27e0b6b1669bf05; Path=/"