Make a GET Request and receive parameters
I am working on a Ruby on Rails App.I have to make a GET request to following:
https://[@subdomain].chargify.com/subscriptions/[@subscription.id].json
The response received is going to be json. I am trying to achieve this by making the GET request using the 'net/http' library of ruby and my code is as follows :
res = Net::HTTP.get(URI.parse('https://[@subdomain].chargify.com/subscriptions/[@subscription_id].json'))
But I am getting an error which is :
bad URI(is not URI?): https://[@subdomain].chargify.com/subscriptions/[@subscription开发者_运维百科_id].json
I am not sure where exactly am I making mistake with this. Any suggestions or help would be appreciated.
Thanks
Do you mean #{@subdomain}
instead of [@subdomain]
? That has to be inside of double quotes "
in order to get interpolated properly.
By now you know that your error was that you didn't interpolate the string correctly. You should have specified #{@subdomain} rather than [@subdomain] and you need double quotes around the string
The problem that you now have is that you need to tell the http connection to use ssl. I use something like this ..
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
Two things:
The value of @subdomain needs to be passed using
#{@subdomain}
The connection is ssl
def net_http(uri) h = Net::HTTP.new(uri.host, uri.port) h.use_ssl=true h end
Then
request=net_http(URI.parse("https://# {@subdomain}.chargify.com/subscriptions/#{@subscription_id}.json"))
To perform get actions
request=net_http(URI.parse("https://# {@subdomain}.chargify.com/subscriptions/#{@subscription_id}.json")).start do |http|
http.get(URI.parse("whatever_url").path,{:params=>123})
end
精彩评论