gpt4 book ai didi

ruby - URI::InvalidURIError 使用 HTTParty 时

转载 作者:数据小太阳 更新时间:2023-10-29 08:45:55 25 4
gpt4 key购买 nike

我遵循了 HTTParty github page 中的示例并想出了这个:

class MatchHistory
include HTTParty
base_uri = "api.steampowered.com/IDOTA2Match_570"

def initialize
@options = { query: { key: STEAM_API_KEY } }
end

def latest
self.class.get("/GetMatchHistory/V001", @options)
end
end

get '/' do
history = MatchHistory.new

history.latest.body
end

我收到以下错误:

URI::InvalidURIError at /
the scheme http does not accept registry part: :80 (or bad hostname?)

但是,当我使用像下面这样的更简单的解决方案时,它返回的结果很好:

class MatchHistory
def initialize
@base_uri = "http://api.steampowered.com/IDOTA2Match_570"
end

def latest
HTTParty.get(@base_uri + "/GetMatchHistory/V001/?key=" + STEAM_API_KEY)
end
end

最佳答案

base_uri 是一个类方法,所以你应该在类中定义它,而不是在你的初始化器中。您可以在您提供的链接中的第一个示例中看到它。

class MatchHistory
include HTTParty

base_uri "api.steampowered.com/IDOTA2Match_570"

def initialize
@options = { query: { key: STEAM_API_KEY } }
end

def latest
self.class.get("/GetMatchHistory/V001", @options)
end
end

关于ruby - URI::InvalidURIError 使用 HTTParty 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28744658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com