Ruby:如何将多字节字符表示为百分比转义八位字节以供 httparty 使用
我正在使用 HTTParty 从某个 REST url 获取 json 数据
require 'httparty'
@search_term = "movies"
@data = HTTParty.get("http://api.douban.com/events?q=#{@search_term}")
问题是,如果我传递包含多字节字符的 get 参数,例如,如果我想使用中文 UTF-8 搜索词“电影”:
@search_term = "电影"
@data = HTTParty.get("http://api.douban.com/events?q={@search_term")
我得到一个错误
URI::InvalidURIError at / bad URI(不是 URI?):链接在此处
在 Stackoverflow 上搜索,我发现了以下 answer 建议将我的 @search_term 格式化为百分比转义的八位字节,但我无法弄清楚如何做到这一点。
我正在寻找一个如下所示的解决方案:
@search_term = params[:search_term]
@search_term = solution_with_some_escaping_or_something(@search_term)
@data = HTTParty.get("http://api.douban.com/events?q={@search_term")
#profit
I'm using HTTParty to get json data from some REST url
require 'httparty'
@search_term = "movies"
@data = HTTParty.get("http://api.douban.com/events?q=#{@search_term}")
The problem is if I pass a get parameter that contains multi-byte characters, for example if I wanted to use a Chinese UTF-8 search term, "电影":
@search_term = "电影"
@data = HTTParty.get("http://api.douban.com/events?q={@search_term")
I get an error
URI::InvalidURIError at / bad URI(is not URI?): link here
Searching on Stackoverflow, I found the following answer which suggest formatting my @search_term to percent-escaped octets, but I haven't been able to figure out how to do so.
I'm looking for a solution which would look like:
@search_term = params[:search_term]
@search_term = solution_with_some_escaping_or_something(@search_term)
@data = HTTParty.get("http://api.douban.com/events?q={@search_term")
#profit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有尝试过使用非英语字符..但是这样的东西应该可以工作。
I haven't tried it with non english characters.. but something like this should work.