66

Is it possible to redirect using a POST method ?
Or should redirects always be made using GET ?

The use for this is in the final steps of an order process for an e-commerce site, to send the data to the payment processor, without introducing an extra step for the user.

andi
  • 14,322
  • 9
  • 47
  • 46

6 Answers6

84

Redirection isn't possible with POST requests - it's part of the HTTP/1.1 protocol.

You could either introduce another step that contains the form data to be POSTed to the payment processor, or you could send the post from your application (something I have done when working with PROTX).

Codebeef
  • 43,508
  • 23
  • 86
  • 119
14

I "solved" the issue by displaying a summary page with all the products and delivery charges etc, with the typical "to confirm and pay for your purchases, click the continue button below" type message. The continue button causes the site to POST the product data and everything across to the payment processor.

The short answer - there is another step for the user. However, the key is to make it seem as natural and as much part of the checkout experience as you can. This way, it doesn't come across too much as "just another step".

However, if you do come across a better way, I'll be very interested to hear what it was :)

Mark Embling
  • 12,605
  • 8
  • 39
  • 53
  • 1
    Instead you can just display "Processing order..." placeholder and autosubmit the form with hidden fields. If the page works fast enough the user wont even see this page most of the time... – mdrozdziel Sep 26 '11 at 07:12
  • 1
    That's pretty much the same as Alsciende's method below. Perfectly fine, but you will be relyin on JavaScript and as such you'll want some sort of fallback for the (admittedly unlikely these days, but possible) case where JS is not available. – Mark Embling Oct 02 '11 at 14:14
  • Yeah, but this is very trivial. Just putting the submit inside noscript should do the trick... – mdrozdziel Oct 03 '11 at 12:17
11

With a simple line of javascript, you can have your POST form to post itself (form.submit()). You can then hide the form and display a simple "please wait while..." message to the user while the form is submitted to the payment processor.

Alsciende
  • 26,583
  • 9
  • 51
  • 67
  • And fall back to the manual 'press the submit button' method where JS is disabled? That's not a bad idea actually... – Mark Embling Jun 12 '09 at 09:55
8

The idea is to make a 'redirect' while under the hood you generate a form with method :post.

I have faced the same problem and extracted the solution into the gem repost, so it is doing all that work for you, so no need to create a separate view with the form, just use the provided by gem function redirect_post() on your controller.

class MyController < ActionController::Base
...
  def some_action
    redirect_post('url', params: {}, options: {})
  end
...
end
yaro
  • 107
  • 1
  • 2
1

html hack: instead redirect_to, render this html template (based on Alsciende answer)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<form id="step" name="step" action="<%= URL %>" method="POST">
  <!-- optional params -->
  <input type='hidden' name='token' value='e7295d6d1cd512a8621f1de5965b90a' />

</form>

<script type="text/javascript">
 $(document).ready(function () {
   setTimeout(function () {
     $("#step").submit();
   }, 0);
 });
</script>
Abel
  • 3,989
  • 32
  • 31
0

If you need to redirect a POST request internally based on payload (for example, when using webhooks on an API for which you do not control the event granularity), you may want to use rack middleware.

You can then "redirect" the request before it is assigned to a route (you aren't technically redirecting the request, just updating the url to point towards the route of your choice). See inspiration

Bruno Degomme
  • 883
  • 10
  • 11