简体   繁体   中英

uninitialized constant HTTPSuccess (NameError)

I am trying to learn some ruby http request response code from this tutorial -

http://danknox.github.io/2013/01/27/using-rubys-native-nethttp-library/

Code so far -

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

error -

Test.rb:16: uninitialized constant HTTPSuccess (NameError)

I saw the only stack overflow post on this issue. Did not help. Why could this be happening ?

This is happening because HTTPSuccess has not been initialized. Try using Net::HTTPSuccess (and Net::HTTPRedirection ) instead.

Also, change your case statement to case response.class . In your when statements you're checking for class equality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM