Question

In Rail my final goal is to write a Net::HTTP client to connect to my REST API that is returning JSON and parse it, pass it to View , etc.... But first things first!
What is the simplest thing I can start with?

I am looking at this page:http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html

and I get the impression that if I have one .rb file with these two lines of code in it, it should show me something?

require 'net/http'
Net::HTTP.get('example.com', '/index.html')
Was it helpful?

Solution

url = URI.parse("http://example.com")
req = Net::HTTP::Get.new(url.path)
@resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}

in a view

<%= "The call to example.com returned this: #{@resp}" %>

OTHER TIPS

You could start testing with something like this:

require 'net/http'
response = Net::HTTP.get_response("www.google.com","/")
puts response.body

I'll recommend you take a look at the docs: Net::HTTPSession

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top