0

There is an exact duplicate of this question that is over four years old here

Given that it's been so long, my question now is, is that answer still accurate? Is there a better way to do this now?

Community
  • 1
  • 1
Peter Berg
  • 6,006
  • 8
  • 37
  • 51

1 Answers1

8

You do something like this. Write a function in your controller like this:

require 'net/http'
require 'net/https'
class custom_class
def get_api_call(args_hash)
    uri = URI.parse("sample_api_url")
    uri.query = URI.encode_www_form(what_args_you_want_to_send)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Post.new(uri.request_uri)
    http.request(request).body
end

private

def what_args_you_want_to_send
{
      "varname1" => var1,
      "varname2" => var2,
      "varname3" => var3,
      "varname4" => var4
}
end

The result of that function will have the answer from the server you are send a request to

Aravind
  • 1,391
  • 1
  • 16
  • 41
  • Thanks, this is really helpful. I'm guessing that setting verify_mode to VERIFY_NONE just makes it so that the request doesn't check to see who signed the site's ssl certificate? – Peter Berg Oct 10 '13 at 09:30
  • Sets the flags for server the certification verification at beginning of SSL/TLS session. I found this here(http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html). – Aravind Oct 10 '13 at 09:50