我试着从本教程中学习一些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
=>"200"
response.body
=> # Raw response body would go here needing to be parsed错误-
Test.rb:13: syntax error, unexpected tASSOC, expecting $end
=> "200"
^我不知道为什么会这样。我删除了空格,错误仍然存在。我看到3-4栈溢出的帖子,但他们没有帮助。
发布于 2014-12-04 21:10:06
您应该注释掉以=>开头的代码中的两行。这些注释解释了方法的返回值应该是什么,但不知怎么的,它们没有注释,Ruby解释器试图将它们解析为代码。
puts response.code # => "200"
puts repsonse.body # => Raw response bodyhttps://stackoverflow.com/questions/27304087
复制相似问题