A
A
Alexander Isaev2016-10-15 19:36:12
ruby
Alexander Isaev, 2016-10-15 19:36:12

How to insert variable into url address?

Hello everyone) Guys, I'm making a request like https://music.yandex.ru/search?text=sia. How to insert a variable instead of the word sia. Let 's say
a = "sia"
uri = URI.join(' https://music.yandex.ru/search?text= ', a) response = Net:: HTTP.get
(uri)
puts JSON.parse(response)
do Json parse of the page. And that doesn't work. Thanks to all)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Shvaikin, 2016-10-15
@IsaevAlex

The URI is smart, and removed everything superfluous, leaving only the host and glued /sia to it.
In your case, it is better to use interpolation, like this

a = 'sea'
URI("https://music.yandex.ru/search?text=#{a}")
#=> #<URI::HTTPS https://music.yandex.ru/search?text=sea>

Or do like this:
uri = URI('https://music.yandex.ru/search')
params = { :text => 'пятница' }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question