Reputation: 1057
I'm using HTTParty for making HTTP requests and for using REST APIs. Now I want to re-use cookies set by a login page I called via POST.
class SomeImporter
include HTTParty
def self.login
response = self.post('https://www.example.com/login', :query => {:user => 'myusername', :password => 'secret'})
self.default_cookies.add_cookies(response.header['set-cookie'])
self.get('https://www.example.com/protected')
end
end
The cookies are not set correctly with this code. How to parse the 'set-cookie' header given by HTTParty correctly and set the cookies for next request?
Upvotes: 5
Views: 4563
Reputation: 1019
Thanks to @dineshspabu, I'm able to follow his pattern and add some modifications to handle expire date. Hope this help:
def cookies_to_hash(set_cookie)
set_cookie = set_cookie.gsub(/Expires=[a-zA-Z]+,/) do |m|
# replace , with #####
m[0..-2] + "#####"
end
set_cookie = set_cookie.split(',')
# Cookies as array of hashes
all_cookies = []
set_cookie.each do |each_cookie|
ecookie = []
each_cookie.strip.split(';').each do |cookie_attr|
# replace ##### with , back
ecookie << cookie_attr.split("=").map{|e| e.gsub(/#####/, ',') if e}
end
all_cookies << Hash[ecookie]
end
all_cookies
end
Upvotes: 1
Reputation: 165
def cookies_to_hash(acookies)
vs = acookies.split(',')
ak = Array.new
# Avoiding break in expiry date comma
vs.each_cons(2) do |v, vn|
unless(vn.include?("="))
ak << "#{v}#{vn}"
else
ak << v if(v.include?("="))
end
end
# Cookies as array of hashes
all_cookies = []
ak.each do |each_cookie|
ecookie = []
each_cookie.split(';').each do |cookie_attr|
ecookie << cookie_attr.split("=")
end
all_cookies << Hash[ecookie]
end
all_cookies
end
The above method will help you convert set-cookies in response headers into a valid array of hashes. Handled problems with additional commas on the expiry.
Upvotes: 1
Reputation: 1057
Usually there is a single entry for each Set-Cookie
within the HTTP header. HTTParty merges them in one single string as comma-separated list. But HTTParty doesn't split them by itself when adding them back to default cookies. You have to parse them by yourself.
It is possible to parse the 'set-cookie' with following method. Add it to your class:
# Parse the 'set-cookie' string
# @param [String] all_cookies_string
# @return [Hash]
def self.parse_set_cookie(all_cookies_string)
cookies = Hash.new
if all_cookies_string.present?
# single cookies are devided with comma
all_cookies_string.split(',').each {
# @type [String] cookie_string
|single_cookie_string|
# parts of single cookie are seperated by semicolon; first part is key and value of this cookie
# @type [String]
cookie_part_string = single_cookie_string.strip.split(';')[0]
# remove whitespaces at beginning and end in place and split at '='
# @type [Array]
cookie_part = cookie_part_string.strip.split('=')
# @type [String]
key = cookie_part[0]
# @type [String]
value = cookie_part[1]
# add cookie to Hash
cookies[key] = value
}
end
cookies
end
The cookies can be added to HTTParty for following requests by adjusting this line:
self.default_cookies.add_cookies(self.parse_set_cookie(response.header['set-cookie']))
Within self.parse_set_cookie
the cookies are extracted only with name and value. You could extend it to get more details like Path
, Domain
and so on. See RFC 2109 (4.2.2 Set-Cookie Syntax) for more details.
Upvotes: 3