Can I POST a form to an external URL and also my own server? - ruby-on-rails

In rails, can I get a form submit to send the info in the form both to an external URL, and also post to my own server, all with one click from the user?

In the create or update actions in the controller, after the save action:
require 'net/http'
Net::HTTP.get('www.example.com', '/index.html?'+params)

You have to do it yourself, preferably by triggering the external request after you're done with your procedure, hopefully using message queuing.

Related

How do you create and add a Zapier Zap action link?

I tried to look for this way of adding a Zap action into an HTML button either for a website or a rich email but I couldn't find one.
Obviosuly, adding a link to a button is very simple; but is there a way to create a such Zapier link? That will trigger a Zap.
I believe webhook will trigger via a GET request with query params. So if you have an HTML button that sends the user to such a URL, it'll kick off an action.
You should also be able to use an onClick handler to call fetch to send a POST request to the webhook url. But, there may be CORS issues with this approach.

Rails: links in email to execute post action

In emails, the only type of action possible is to send link (GET method).
What is the correct approach to send a link that will execute a POST action in the app (Eg. accept friendship)?
I see two possible solutions:
custom GET action only used in the email (eg. /action?type=accept_friendship&user_id=10)
get to a page and execute javascript on load to execute the action (eg. what Twitter does to follow back a user from an email)
How does those solutions compare? Are there others?
Thanks
A link with a GET action is ok as long as you include a unique token in the url to confirm validity & origin of the action

How to change GET to POST

I have one web application running on Struts. User is hitting below url directly from browser -
http://:/Test/servlet.do?name=XX&address=YYY
By default this request is submitted as GET by the user. Now my question is
1) how do I change user request to POST?
Use JQuery post or ajax methods, using an empty form with hidden keys.
There are plenty of browser add-ons that will let you send your request as POST (such as DHC by Restlet and Advanced REST client for Chrome)
Remember GET is inside the ans is set into links. POST are usually done by , or GET is possible too. So hitting a URL or link as you said is not possible to do over html, use PHP or Jquery so have it GET instead .

Rails: how to simulate a form submission from inside a controller method?

I'm working on a Ruby on Rails project where we want to automate some form submissions. How do we simulate posting a form from inside a controller method?
You could follow the POST below to use net/http to do a form post to an external or internal HTTP end point.
Submitting POST data from the controller in rails to another website
or you could use a popular HTTP client or REST client like httparty, wrest, mechanize etc. Look at the list here. https://www.ruby-toolbox.com/projects/rest-client

Ruby on Rails POST parameters on redirect_to

I have to make a call to a different url in one of my controllers on my site. The problem is that with all the parameters the other site requires I'm overflowing the url. Is there anyway to call another url from the controller and send all of the parameters using a POST?
I'm not expecting a response from the other site. Also, I think there's a way to do this using the Net::HTTP library thought I'm not sure how.
Thanks
You can't do a redirect and send POST data at the same time in the HTTP spec. Redirects are implemented by sending a simple Location: otherlocation.html header. POST data doesn't fit anywhere into that system.
Do you want the user to go to this page, or do you want to just send the data to the application yourself? If you want to send the data and not send the user there, use Ruby's Net::HTTP module. If you want to send the user, you may be forced to output a view with a form, and submit it automatically with Javascript. (Don't forget to degrade gracefully by offering a submit button in noscript tags.)
This is from the ruby docs:
require 'net/http'
require 'uri'
result = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
{'q'=>'ruby', 'max'=>'50'})
As you can see, you pass the params in as a convenient hash, unlike other languages that make you mess with http formatting.
You can also use the flash to transfer the information.
http://guides.rubyonrails.org/action_controller_overview.html#the-flash

Resources