如果电子邮件在 Ruby 中包含到 %2B 的“+”,如何对电子邮件进行编码

How to encode Email if it contains "+" to %2B in Ruby

如何使用 UTF8 编码对 URL 参数进行编码: URL?email=test+@gmail.com 到 URL?email=test%2B@gmail.com

我尝试了 'test+@gmail.com'.encode("UTF-8")

CGI::escape('test+@gmail.com') 它 returns 'test%2B%40gmail.com 但是我需要 test%2B@gmail.com

email=test+@gmail.com 应编码为“+”,其余部分保持不变 URL?email=test%2B@gmail.com

uri std-lib 有一个方法 URI::Escape#escapeURI扩展了URI::Escape模块,所以也有这个方法。

URI.escape('test+@gmail.com', '+')
#=> "test%2B@gmail.com"        ^ the characters to escape with URL encoding

不过喜欢

Why do you want to encode the + in the URL but not the @? @ must be encoded too.