存储带引号的cookie时的编码问题

时间:2023-01-11 20:17:29

I need to create a cookie containing double quotes.

我需要创建一个包含双引号的cookie。

cookies[:mycookie] = '"contentofmycookie"'

But then my cookie actually contains:

但是我的cookie实际上包含:

%22contentofmycookie%22

I've seen that Rails encodes cookie values; may be I should avoid this by overriding something but I don't know what. I'm using Rails 3.2.3.

我已经看到Rails编码cookie值;可能是我应该通过覆盖一些事来避免这种情况,但我不知道是什么。我正在使用Rails 3.2.3。

Edit: I need my cookie to contain double quotes to get it recognized by another application. This other application is not a Rails application and uses a cookie with quotes inside.

编辑:我需要我的cookie包含双引号,以使其被另一个应用程序识别。这个其他应用程序不是Rails应用程序,并使用带引号的cookie。

Thanks for your help!

谢谢你的帮助!

1 个解决方案

#1


1  

You need to unescape your cookie when you extract it.

提取它时,您需要取消对cookie的访问。

require  'cgi'
print CGI.unescape cookies[:mycookie]
#=> "contentofmycookie"

#1


1  

You need to unescape your cookie when you extract it.

提取它时,您需要取消对cookie的访问。

require  'cgi'
print CGI.unescape cookies[:mycookie]
#=> "contentofmycookie"