1

My code is

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

and params[:content] is "안녕" now.

if i run this code,

I get following error

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)

How can i send Korean string in URL?

(.encode("utf-8) is not working..)
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
한승우
  • 25
  • 2

1 Answers1

2

As the error message says "URI must be ascii only", you may encode Korean to URI format as below.

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

Further info lies here: Ruby url encoding string

qpzm
  • 428
  • 1
  • 5
  • 13