Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing HTTP header 'set-cookie' with HTTParty

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?

like image 730
Sven R. Avatar asked Mar 31 '16 09:03

Sven R.


1 Answers

Usually there is a single entry for each Set-Cookiewithin 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.

like image 125
Sven R. Avatar answered Oct 29 '22 02:10

Sven R.