【问题标题】:not an HTTP URI不是 HTTP URI
【发布时间】:2021-05-03 11:07:16
【问题描述】:

我在我从邮递员那里得到的代码上使用 ruby​​ on a rails 调用 circle.so api。

require "uri"
require "net/http"

url = URI("hub.atlas.fm/api/v1/community_members")

http = Net::HTTP.new(url.host, url.port);
request = Net::HTTP::Get.new(url)
request["Authorization"] = "phdbM12mVAiEyLF19UjoYdiU"

response = http.request(request)
puts response.read_body

虽然在邮递员中完美运行,但它不适用于我的应用程序,错误为

ArgumentError in TopicsController#index
not an HTTP URI

我该如何解决这个问题?

【问题讨论】:

标签: ruby-on-rails ruby


【解决方案1】:

您需要在您的网址字符串中指定http://

url = URI("http://hub.atlas.fm/api/v1/community_members")

如果省略方案 (http://) 部分,那么 ruby​​ 不知道这是什么特定的 URI 类型。

2.6.3 :005 > url = URI("hub.atlas.fm/api/v1/community_members")
 => #<URI::Generic hub.atlas.fm/api/v1/community_members>

但如果你指定方案 (http://) 那么你会得到

2.6.3 :003 > url = URI("http://hub.atlas.fm/api/v1/community_members")
 => #<URI::HTTP http://hub.atlas.fm/api/v1/community_members>

注意不同的类(URI::GenericURI::HTTP)。

还有很多其他类型的 URI,例如:

2.6.3 :007 > url = URI("file://hub.atlas.fm/api/v1/community_members")
 => #<URI::File file://hub.atlas.fm/api/v1/community_members>
2.6.3 :008 > url = URI("ftp://hub.atlas.fm/api/v1/community_members")
 => #<URI::FTP ftp://hub.atlas.fm/api/v1/community_members>

您可以在这里找到更多信息:https://ruby-doc.org/stdlib-2.7.2/libdoc/uri/rdoc/URI.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-05
    • 2016-08-25
    • 2012-02-23
    • 2011-07-19
    • 2012-04-09
    • 2013-10-15
    • 2014-11-17
    • 1970-01-01
    相关资源
    最近更新 更多