Submit Post Data from Controller to Another Website in Rails

Submit POST data from controller to another website in Rails

The simpliest way is using ruby core library:

require "uri"
require "net/http"

params = {'box1' => 'Nothing is less important than which fork you use. Etiquette is the science of living. It embraces everything. It is ethics. It is honor. -Emily Post',
'button1' => 'Submit'
}
x = Net::HTTP.post_form(URI.parse('http://www.interlacken.com/webdbdev/ch05/formpost.asp'), params)
puts x.body

Pro Tip: Do an asynchronous request, using a gem like delayed_job or background_rb

Submitting a POST request from a rails controller to another site

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

Submitting a POST request from a rails controller to another site

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

How can a controller respond with an action to redirect from another website in Ruby

This is just the basics of how to post a form to an external website. You'll have to fill in the details yourself since I have no clue what the external website does or what that script is supposed to accomplish.

Setting up a form to post to a remote url is pretty easy:

# Rails 4 and earlier
<% form_tag("http://example.com/test.php", method: :post) %>
<%= hidden_field_tag :key, ENV["EXAMPLE_PUBLIC_KEY"] %>
<%= hidden_field_tag :title, current_user.books.title %>
<%= hidden_field_tag :ref, SecureRandom.random_number %>
<%= submit_tag %>
<% end %>

# Rails 5+
<%= form_with(url: "http://example.com/test.php", method: :post) do |f| %>
<%= f.hidden_field :key, value: ENV["EXAMPLE_PUBLIC_KEY"] %>
# ...
<% end %>

Handling the callback from the external website isn't really different then any other route in your rails app:

get '/some/path', to: 'foo#bar'

You just create a route that responds to GET, and configure whatever service that handles the form submission to redirect back to it.

Infrastructure question: How to send/receive data to another website with my rails application?

you should create api by the following steps:

  1. in the form of website, user fill information and submit them
  2. in the submit action it should call your Rails API (you should
    create a controller Api::BikesController and an action for example
    bikes_info). bikes_info API called.
  3. bikes information sent in response of API
  4. web sile get the information display to the user and user select one
    of that option and click submit button and second API called from
    your app
  5. "reserve" is the second API name in your app. you should get
    reservation info and save data in DB and send response to the web
    site

Send POST request to another action

No need to post to another action here, just follow the rails pattern (if you've ever scaffolded a new resource, you should have seen this before):

def download
url = Url.new(params[:url])
if url.save
redirect_to happy_route
else
redirect_to unhappy_route
end
end

Post data from form to different controller and redirect back

It looks like you're trying to generate a URL in your form_for by specifying the controller and the action but you're not mapping this route in your routes.rb.

So you have two options. The first is to 'hardcode' the URL in the form_for, like this:

<%= form_for @url, :url => "/url/create", :html => {:method => :post} do |f| %>

But a much better solution would be to get your routes into RESTful shape.

You could do this:

Ssurl::Application.routes.draw do
get 'shorturl' => 'landingpage#shorturl', :as => :shorturl
resource :url, :only => [:create]
root :to => 'landingpage#index', :as => :landingpage
end

Which will create a RESTful create route using the POST http method. So your form_for would then look like this:

<%= form_for @url do |f| %>

Much cleaner!



Related Topics



Leave a reply



Submit