Search code examples
ruby-on-railsrubyrestrest-client

Ruby- web api with rest client gem


I'm new in Ruby (and in developing too), and I would like to get the response from another url (my url) in the get method.

I'm using rest-client gem.

I have tried this code:

class UsersController < ApplicationController
  require 'rest-client'
  def index
    RestClient::Request.execute(method: :get, url: 'https://my-url')
  end
end

But when I open http://localhost:3000/users, I get a blank page. Can anyone help me understand why?


Solution

  • You should render the response text:

    require 'rest-client'
    
    class UsersController < ApplicationController
      def index
        render text: RestClient::Request.execute( method: :get, url: 'https://my-url').to_str, layout: nil
      end
    end