Answer the question
In order to leave comments, you need to log in
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
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>
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 questionAsk a Question
731 491 924 answers to any question