1

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

Splashlin
  • 7,225
  • 12
  • 46
  • 50
  • possible duplicate of [redirect\_to using POST in rails](http://stackoverflow.com/questions/985596/redirect-to-using-post-in-rails) – Brad Werth Aug 13 '15 at 16:24

3 Answers3

4

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.)

Matchu
  • 83,922
  • 18
  • 153
  • 160
  • I want to send the user there. So basically I have to send the user to a view and then do a JS call on the page load to POST the information to the site? – Splashlin Jan 27 '10 at 03:42
  • Pretty much. It's hackish, but it's what you have available to you. – Matchu Jan 27 '10 at 14:36
2

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.

Jaime Bellmyer
  • 23,051
  • 7
  • 53
  • 50
  • 1
    This submits the data to the other website, but does not send the end-user there, as the OP requires. – Matchu Jan 27 '10 at 19:56
  • The OP specifically said "I'm not expecting a response from the other site." This sounded like an API call to me. He later clarified that he does want to send the user there, so he must mean that he's not expecting the *user* to return. But I'd already posted by then. – Jaime Bellmyer Jan 27 '10 at 21:41
2

You can also use the flash to transfer the information. http://guides.rubyonrails.org/action_controller_overview.html#the-flash

B_.
  • 2,164
  • 2
  • 17
  • 20