簡體   English   中英

其余客戶端Ruby中的Cookie

[英]Cookie in rest-client Ruby

是否可以添加Cookie,例如

key[index]=value

這是我的代碼

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

這段代碼可以正常工作,但是當我嘗試類似

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

我在/../../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基本上只能存儲字符串(對於每個第一級密鑰)。

為了將散列存儲在cookie值中,您需要對數據進行序列化/反序列化,例如, 使用JSON

cookies[:i_need_to_store_hash] = hsh.to_json

然后

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

但請記住,cookie的大小非常有限(4096字節)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM