如何在Ruby中获取http请求的内容?

Rya*_*nce 13 ruby ruby-on-rails http

在PHP中我可以这样做:

$request = "http://www.example.com/someData";
$response = file_get_contents($request);
Run Code Online (Sandbox Code Playgroud)

我如何在Ruby(或一些Rails方法?)中做同样的事情?

我一直在谷歌搜索半小时,然后完全缩短.

gle*_*man 20

open-uri您正在使用标准库包:

require 'open-uri'
contents = open('http://www.example.com') {|io| io.read}
# or
contents = URI.parse('http://www.example.com').read
Run Code Online (Sandbox Code Playgroud)


Rya*_*nce 11

require 'net/http'
Net::HTTP.get(URI.parse('http://www.example.com/index.html'))
Run Code Online (Sandbox Code Playgroud)

不知道为什么我之前没有找到这个.除非有更好的方法,否则我会选择这个!