Redirecting an HTTP POST

2019-01-19 08:21发布

Ok, so in my web app's API I have an incoming HTTP post request. I would like to pass that POST request on to a different server, without losing the data in the POST header. Is this possible? which type of redirect would I use? php examples?

Edit: The HTTP request is coming from a mobile app, not a web browser.

Thanks!

8条回答
欢心
2楼-- · 2019-01-19 08:53
RewriteRule current-page.php http://www.newserver.com/newpage.php [NC,P]

The P on there (proxy) will preserve the POST data. You'll need to turn on the apache proxy module if it isn't already.

查看更多
Bombasti
3楼-- · 2019-01-19 08:55

You cannot tell a browser to make a post request through an HTTP header. The location header will redirect, but only for GET or HEAD requests.

You can work around this limitation by displaying a page with a hidden form with the method attribute set to POST and the action set to the URL you want the browser to post to, then automatically submit it on page load. Example:

<body onload="document.getElementById('form').submit();">
  <form id="form" action="http://example.com/form_handler.php" method="POST">
    <input type="hidden" name="param_1" value="data">
  </form>
</body>

Alternately, you can make the POST request on your server and then display the results.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-19 08:58

If you want to take data from a POST request and simply POST it to another server, then use cURL.

--or--

If you want to take data from a POST request and redirect the client to that other server while POSTing the data, then use this method...

Dynamically generate a form with all of the POST data. Something likes this...

echo "<form name=\"someform\" action=\"http://www.somewhereelse.com/someform.whatever\">";
foreach ($_POST as $key=>$value) {
    echo "<input type=\"hidden=\" name=\"" . htmlspecialchars($key) . "\" value=\"" . htmlspecialchars($value) . "\" />";
}
echo "</form>";

Then, submit that form with some JavaScript when the page is done loading...

document.forms['someform'].submit();
查看更多
该账号已被封号
5楼-- · 2019-01-19 08:59

I used the following code to redirect a post. In my case I am using only application/octet-stream content type so make sure you take that into consideration.

$request = file_get_contents ( "php://input" ); 

$arrContextOptions=array(
        "http" => array(
                "method" => "POST",
                "header" =>
                'Content-Type: application/octet-stream'. "\r\n".
                'Content-Length: ' . strlen($request) . "\r\n",
                "content" => $request,
        ),
        "ssl"=>array(
                "allow_self_signed"=>true,
                "verify_peer"=>false,
        ),
);
$arrContextOptions = stream_context_create($arrContextOptions);


header ( "HTTP/1.1" );
header ( "Content-Type: application/octet-stream" );

$result =  file_get_contents('http://thenewaddress.yes.it.works', false, $arrContextOptions);

file_put_contents("php://output", $result);
查看更多
Deceive 欺骗
6楼-- · 2019-01-19 09:02

You could use cURL or sockets to re-post the data, but you can't really redirect it.

POST'ing to a URL with cURL:

$ch = curl_init('http://www.somewhere.com/that/receives/postdata.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
查看更多
地球回转人心会变
7楼-- · 2019-01-19 09:07

If the client (ie the mobile app) HTTP library supports this, then you can return HTTP 307 from server which states that "the request should be repeated with another URI ... with the same method". This is essentially a temporary redirect but tells the client to use the the same method, a POST.

The client making the request must be able to respond accordingly to the HTTP 307 response and follow the redirection with the same method - for many libraries this may be an additional flag or setting.

查看更多
登录 后发表回答