我如何在 URL 中发送韩语字符串?(Ruby 在 Rails 上使用 HTTParty gem)

How can i send Korean string in URL?(Ruby on Rails using HTTParty gem)

我的密码是

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..)

如错误消息所述"URI must be ascii only",您可以将韩语编码为 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