Ruby Parsing: Error when I trying to put Cyrillic symbols into URL request

1.2k Views Asked by At

I wrote a Bot for Telegram, where users can receive images for their requests. But there was one problem, which I could not solve.

Some example with parsing on Ruby:

json_object = JSON.parse(open("https://api.site.com/search/photos?query=" + message.text + "&per_page=10&client_id=42324d2lkedi234fs342dfse2c038fdfsdfs").read)

message.text - It's a field with request from users.

Everything works fine with latin literals, but when I send Cyrillic(API also supports Cyrillic alphabet) symbols I get the below error:

/Users/me/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/uri/rfc3986_parser.rb:21:in `split': URI must be ascii only "https://api.site.com/search/photos?query=\u0432\u0430\u0432\u0430&per_page=10&client_id=42324d2lkedi234fs342dfse2c038fdfsdfs" (URI::InvalidURIError)

I used Encoding with utf-8 and win-1252, but nothing helped. How should this be fixed?

2

There are 2 best solutions below

0
On BEST ANSWER

You should encode your cyrillic string:

URI.encode('http://google.com?1=АБВ') # => "%D0%90%D0%91%D0%92"

So, use it like this (or encode whole url):

URI.encode(message.text)
0
On

Try with

"anything".parameterize.underscore.humanize.downcase