1

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

Smoke
  • 1,052
  • 1
  • 10
  • 24
  • I followed the instructions and now my code reads like this : @res = Net::HTTP.get(URI.parse("https://#{@subdomain}.chargify.com/subscriptions/#{@sub‌​scription_id}.json")) Now in my view when I do debug(@res) It is giving me this error: --- | 400 The plain HTTP request was sent to HTTPS port

    400 Bad Request

    The plain HTTP request was sent to HTTPS port

    nginx/0.7.66
    Any idea how do I resolve this issue ?? Thanks
    – Smoke Jul 07 '11 at 21:34

3 Answers3

1

Do you mean #{@subdomain} instead of [@subdomain]? That has to be inside of double quotes " in order to get interpolated properly.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • like this - Net::HTTP.get(URI.parse("https://#{@subdomain}.chargify.com/subscriptions/#{@subscription_id}.json")) – Brian Glick Jul 07 '11 at 19:48
  • Yeah, that's exactly it. `#{...}` will evaluate the contents and convert it to a string, calling `.to_s` as required. – tadman Jul 07 '11 at 19:58
1

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)
Chris McCauley
  • 25,824
  • 8
  • 48
  • 65
  • No I am not getting the json response like this or may be I am doing it wrong. Here is my code : uri = URI.parse("https://#{@subdomain}.chargify.com/subscriptions/#{@subscription_id}.json") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true @res = Net::HTTP::Get.new(uri.request_uri) And here is the error I am getting now : --- !ruby/object:Net::HTTP::Get body: body_stream: header: accept: - "*/*" method: GET path: /subscriptions/660668.json request_has_body: false response_has_body: true Not sure now what is causing this ? – Smoke Jul 07 '11 at 22:26
  • Silly question (sorry) but did you call @res.response.body ? It's a bit late here so my thinking is fuzzy so maybe that's what you mean. – Chris McCauley Jul 07 '11 at 22:54
0

Two things:

  1. The value of @subdomain needs to be passed using

    #{@subdomain}
    
  2. 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 
rightskewed
  • 624
  • 2
  • 11
  • 24