class ApiController < ApplicationController
def index
render json: { data: "Hello from API" }
end
end
require 'net/http'
require 'uri'
url = URI.parse('http://localhost:3000/api')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res.body
此代码将向第一个应用程序的API发送GET请求,并将响应输出到控制台。