【问题标题】:How can i send Korean string in URL?(Ruby on Rails using HTTParty gem)如何在 URL 中发送韩语字符串?(Ruby on Rails 使用 HTTParty gem)
【发布时间】:2018-04-01 13:12:01
【问题描述】:

我的代码是

resp = HTTParty.get("http://sandbox.api.simsimi.com/request.p?key=e7501386-fca8-4723-b278-36755e917526&lc=ko&ft=1.0&text=#{params[:content]}")

params[:content] 现在是"안녕"

如果我运行这段代码,

我收到以下错误

URI must be ascii only "http://sandbox.api.simsimi.com/request.p?key=e7501386-fca8-4723-b278-36755e917526&lc=ko&ft=1.0&text=\u00ED\u0095\u0098\u00EC\u009D\u00B4" (URI::InvalidURIError)

如何在 URL 中发送韩文字符串?

(.encode("utf-8) is not working..)

【问题讨论】:

标签: ruby-on-rails ruby http httparty


【解决方案1】:

由于错误消息说“URI 必须是 ascii”,您可以将韩语编码为 URI 格式,如下所示。

require 'uri'
str = "안녕".force_encoding('ASCII-8BIT') # "\xEC\x95\x88\xEB\x85\x95"
URI::encode(str) # %EC%95%88%EB%85%95

更多信息在这里:Ruby url encoding string

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多