views:

532

answers:

2

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

+1  A: 

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
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
Pretty much. It's hackish, but it's what you have available to you.
Matchu
A: 

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
This submits the data to the other website, but does not send the end-user there, as the OP requires.
Matchu
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