简体   繁体   English

其余客户端Ruby中的Cookie

[英]Cookie in rest-client Ruby

Is it possible to add cookie like 是否可以添加Cookie,例如

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) 我在/../../util.rb:16:in 'unescape': undefined method 'tr' for #<Hash:0x0000000195f7d0> (NoMethodError)得到了/../../util.rb:16:in 'unescape': undefined method 'tr' for #<Hash:0x0000000195f7d0> (NoMethodError)

Cookies basically can only store strings (for each first-level key). Cookies基本上只能存储字符串(对于每个第一级密钥)。

In order to store hash in cookies values, you need serialize/deserialize the data, for example, with JSON 为了将散列存储在cookie值中,您需要对数据进行序列化/反序列化,例如, 使用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) 但请记住,cookie的大小非常有限(4096字节)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM