2

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"!

Arihant Godha
  • 1,982
  • 2
  • 22
  • 49
  • 2
    Exact duplicate of http://stackoverflow.com/questions/4581075/how-make-a-http-get-request-using-ruby-on-rails. – Todd A. Jacobs Jun 11 '12 at 15:07
  • Sorry @CodeGnome havent seen that I have got the answer you need-- `code`require 'net/http' and then `code` Net::HTTP::Get.new("url") – Arihant Godha Jun 12 '12 at 13:34

3 Answers3

1

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

abhas
  • 5,053
  • 1
  • 30
  • 55
  • thanks for answering @abhas but I am a new bee to rails so I didn't know where to put that in my routes and how to pass that into an action.. – Arihant Godha Jun 11 '12 at 14:23
1

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
anaptfox
  • 81
  • 1
  • 5
1

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
Todd A. Jacobs
  • 71,673
  • 14
  • 128
  • 179