我正在尝试从本教程中学习一些ruby http请求响应代码 -
http://danknox.github.io/2013/01/27/using-rubys-native-nethttp-library/
到目前为止代码 -
require "net/http"
require "uri"
uri = URI.parse("http://api.random.com")
http = Net::HTTP.new(uri.host, uri.port)
# Continuing our example from above
request = Net::HTTP::Get.new("/search?question=somequestion")
response = http.request(request)
# response.code
# response.body
case response
when HTTPSuccess
response.body
when HTTPRedirect
follow_redirect(response) # you would need to implement this method
else
raise StandardError, "Something went wrong :("
end
错误 -
Test.rb:16: uninitialized constant HTTPSuccess (NameError)
我在这个问题上看到了唯一的堆栈溢出帖子。没有帮助。为什么会发生这种情况?
答案 0 :(得分:1)
这种情况正在发生,因为HTTPSuccess
尚未初始化。请尝试使用Net::HTTPSuccess
(和Net::HTTPRedirection
)。
另外,将您的案例陈述更改为case response.class
。在你的when语句中,你正在检查类的相等性。