1

我正在开发 Ruby on Rails 应用程序。我必须向以下人员发出 GET 请求:

https://[@subdomain].chargify.com/subscriptions/[@subscription.id].json

收到的响应将是 json。我试图通过使用 'net/http' ruby​​ 库发出 GET 请求来实现这一点,我的代码如下:

res = Net::HTTP.get(URI.parse('https://[@subdomain].chargify.com/subscriptions/[@subscription_id].json')) 

但我收到一个错误:

错误的 URI(不是 URI?):https://[@subdomain].chargify.com/subscriptions/[@subscription_id].json

我不确定我到底在哪里犯了这个错误。任何建议或帮助将不胜感激。

谢谢

4

3 回答 3

1

你的意思是#{@subdomain}代替[@subdomain]?这必须在双引号"内才能正确插值。

于 2011-07-07T19:37:00.137 回答
1

现在你知道你的错误是你没有正确地插入字符串。您应该指定 #{@subdomain} 而不是 [@subdomain] 并且您需要在字符串周围加上双引号

您现在遇到的问题是您需要告诉 http 连接使用 ssl。我用这样的东西..

http = Net::HTTP.new(uri.host, uri.port)

http.use_ssl = true

request = Net::HTTP::Get.new(uri.request_uri)
于 2011-07-07T21:42:36.550 回答
0

两件事情:

  1. @subdomain 的值需要使用

    #{@subdomain}
    
  2. 连接是ssl

    def net_http(uri)
    h = Net::HTTP.new(uri.host, uri.port)
    h.use_ssl=true
            h
    end
    

然后

     request=net_http(URI.parse("https://#       {@subdomain}.chargify.com/subscriptions/#{@subscription_id}.json"))

执行获取操作

     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 
于 2011-07-08T04:42:04.227 回答