如何查看HTTParty get请求中的响应cookie?

时间:2014-07-26 19:31:20

标签: ruby rest-client httparty

我正在使用httparty向某个网站发出GET请求。我需要响应cookie才能成功发出POST请求才能登录。

rest_client让事情变得非常简单,我所要做的就是:

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"
}

但我想对httparty做同样的事情。所以我的问题是如何在httparty中查看/访问来自GET请求的响应cookie?

1 个答案:

答案 0 :(得分:3)

由于cookies是响应标头的一部分,您可以从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=/"