Arihant Godha
Arihant Godha

Reputation: 2459

rails - how to make a GET request to a url

Hello there I'm hoping this problem is a fairly simple one as I am relatively new to rails development. I am trying to make a get request to a URL.For Example on success on certain action I need to make a get request to a URL with my parameters

http://abc.com/xyz.php?to=<%user.number%>&sender=TESTHC&message="MY MESSAGE"!

Upvotes: 2

Views: 4958

Answers (3)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

Your question, as posed, has nothing to do with Rails or routes. If you want to make an HTTP request, whether from a Rails controller or a standalone Ruby program, you can use a standard library such as open-uri.

As an example:

require 'open-uri'
to, sender, message = 12345, 'foo', 'bar'
uri='http://abc.com/xyz.php?to=%d&sender=%s&message="%s"' % [to, sender, message]
open(uri).readlines

Upvotes: 1

anaptfox
anaptfox

Reputation: 81

This might help. There is a gem called Faraday. It is used to make http request. Here is an example usage:

Build a connection

conn = Faraday.new(:url => 'http://sushi.com') do |builder|
  builder.use Faraday::Request::UrlEncoded  # convert request params as "www-form-urlencoded"
  builder.use Faraday::Response::Logger     # log the request to STDOUT
  builder.use Faraday::Adapter::NetHttp     # make http requests with Net::HTTP

  # or, use shortcuts:
  builder.request  :url_encoded
  builder.response :logger
  builder.adapter  :net_http
end

Make your Get request

conn.get '/nigiri', { :name => 'Maguro' } # GET /nigiri?name=Maguro

Upvotes: 1

abhas
abhas

Reputation: 5213

read this http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html you will get good examples to refer.

Upvotes: 1

Related Questions