Redirect With Method POST
This article describes how to post to a web page (or software) with method POST when you can't use method POST.
That statement likely appears to be contradictive. But there is a way to do it.
Why would you want something like this? You might never want it. But if you need to link to a web page that will only accept method POST, this article presents a technique to accomplish it.
Note: See Methods GET and POST for a quick review about the differences.
Send the browser to a page with method GET — which would be through a link, a bookmark, or by typing the URL into the browser's address bar. The page then redirects the browser to the desired destination with method POST.
The redirecting is done with an invisible self-submitting form.
If the browser arrives at the page that does the redirecting and it has parameters, the parameters will be inserted into the invisible self-submitting form.
(A parameter is data following a question mark in the URL. Example:
https://example.com/page.php?name=will
where name=will
is the parameter.)
You can also put custom hidden fields into the invisible form. The custom hidden fields may contain any information that a form can submit. That's because it is a regular form, just invisible.
If the browser arrives at the page that does the redirecting with no parameter in the URL, and there are no custom hidden fields, an empty form will be submitted.
In the source code of the page that does the redirecting are a few lines of PHP code, the HTML form, and a bit of JavaScript.
Here is the source code. Notes follow.
<?php $ParameterFields = ''; $Parameters = array(); foreach( $_GET as $k => $v ) { $Parameters[] = "<input type='hidden' name='$k' value='$v'>"; } $ParameterFields = implode('',$Parameters); ?> <form id="the-redirect-form" method="post" action="https://www.willmaster.com/possibilities/demo/dumpPOST.php" enctype="multipart/form-data"> <?php echo($ParameterFields) ?> <input type="hidden" name="custom" value="Something custom"> </form> <script type="text/javascript"> document.getElementById("the-redirect-form").submit(); </script>
Notes
In the above source code, only the blue-colored text and the red-colored text need attention. The rest can be pasted as-is into the page that does the redirecting.
https://www.willmaster.com/possibilities/demo/dumpPOST.php
is both the URL the form will submit to and the page the browser will be redirected to. It is the destination URL. Change the URL to the correct destination URL.
You may use https://www.willmaster.com/possibilities/demo/dumpPOST.php as the destination URL for your testing — if you don't immediately have your own destination script or web page ready. That destination URL will print all POST data it received so you can review it.
<input type="hidden" name="custom" value="Something custom">
is a custom hidden field. It is constructed like a regular form hidden field. Custom hidden fields are optional. There is no practical limit how many custom hidden fields the form may have.
With the blue-colored text and the red-colored text in the above source code taken care of, it is good to go. Paste it into an otherwise blank page. Make a note of its URL.
To test, load the page that does the redirecting into your browser. The browser will be redirected to the specified destination page.
You can link to the page with the redirect code in order to redirect a browser to the destination page with method POST. When you link to the page with the redirect code, the URL may optionally contain parameters. The parameters will be sent to the destination along with the rest of method POST.
(This content first appeared in Possibilities newsletter.)
Will Bontrager