我正在使用httparty对某个网站执行get请求。我需要响应cookies才能成功地发出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?

最佳答案

因为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=/"

关于ruby - 如何查看来自HTTParty get请求的响应cookie?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24974786/

10-13 06:40