Search code examples
rubyrest-client

Cookie in rest-client Ruby


Is it possible to add cookie like

key[index]=value

Here is my code

options[:headers][:cookies] = {:cookie_key => 'cookie_value'}
resource[options[:path]].get(options[:headers])

This code works fine but when I try something like

options[:headers][:cookies] = {:cookie_key => {} }

I get /../../util.rb:16:in 'unescape': undefined method 'tr' for #<Hash:0x0000000195f7d0> (NoMethodError)


Solution

  • Cookies basically can only store strings (for each first-level key).

    In order to store hash in cookies values, you need serialize/deserialize the data, for example, with JSON

    cookies[:i_need_to_store_hash] = hsh.to_json
    

    and than

    i_need_to_get_my_hash_back = JSON.parse(cookies[:i_need_to_store_hash])
    

    but remember that cookies very limited in size (4096 bytes)