发出 GET 请求并接收参数

发布于 2024-11-19 03:16:59 字数 557 浏览 1 评论 0原文

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

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

收到的响应将是 json。我试图通过使用 ruby​​ 的“net/http”库发出 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

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2024-11-26 03:16:59

您的意思是 #{@subdomain} 而不是 [@subdomain] 吗?它必须位于双引号 " 内才能正确插值。

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

冷弦 2024-11-26 03:16:59

现在您知道您的错误是您没有正确插入字符串。您应该指定 #{@subdomain} 而不是 [@subdomain],并且您需要用双引号将字符串括起来。

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

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

http.use_ssl = true

request = Net::HTTP::Get.new(uri.request_uri)

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)
痴情换悲伤 2024-11-26 03:16:59

两件事:

  1. @subdomain 的值需要使用

    传递

    <前><代码>#{@子域}

  2. 连接是ssl来传递

    def net_http(uri)
    h = Net::HTTP.new(uri.host, uri.port)
    h.use_ssl=true
            小时
    结尾
    

然后

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

要执行get动作

     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 

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文