我正在尝试连接到 API 并使用我的 Rails 应用检索 json 结果,但它似乎不起作用。
举个例子:
@request = Net::HTTP::Get.new "http://example.com/?search=thing&format=json"
当我在浏览器中尝试 url 时,它有效!我得到 JSON 数据,但是当我在 Ruby 中尝试时,正文为零。
>> y @request
--- !ruby/object:Net::HTTP::Get
body:
body_stream:
header:
accept:
- "*/*"
user-agent:
- Ruby
method: GET
path: http://example.com/?search=thing&format=json
request_has_body: false
response_has_body: true
有什么想法吗?
最佳答案
我通常使用open-uri
require 'open-uri'
content = open("http://your_url.com").read
`
关于ruby-on-rails - Ruby Net::HTTP::Get 和 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3647221/