0

Just started learning Slim3. Have been spending some time figuring out how to perform redirects with overriding original request type with no success.

I want the /origin route to perform the redirect to /dest route. /origin route receives GET request performs validation and after success redirects with POST request to /dest uri route. Here is the screenshot. I think I am doing something dumb here:

$app->get('/origin', function($req,$res,$args)
{
    $req= $req->withHeader('X-Http-Method-Override','POST');
    return $res->withRedirect('/dest');
});

$app->post('/dest', function($req,$res,$args)
{
    echo "this is destination page";
});
vai bai
  • 1
  • 2
  • You cant redirect/change a clients GET request into a POST, /origin should be a post, then its possible – Lawrence Cherone Dec 03 '19 at 08:24
  • Please don't ever post pictures of code, it makes it hard to read, and we can't copy it to replicate your problem. Please edit your question and paste in the actual code text instead. Make sure to use ``` tags to format it as code. – Plutian Dec 03 '19 at 08:32
  • Lawrence, appreciate your response. However, I am confused about the solution provided by the documentation suggesting that original request method can be overridden by using either by 1)including _METHOD parameter in the body of a request 2) a custom X-Http-Method-Override HTTP request header. – vai bai Dec 03 '19 at 08:42
  • This is not specific to a framework or language. Please read this question for more information https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect – Nima Dec 03 '19 at 10:15

1 Answers1

0

As noted in the comment, this is not possible as the request made by the browser is not in your control.

When you call ->withRedirect() you are sending a status code of 302 and a Location header to the HTTP client (web browser usually).

The web browser sees the 302 status code and then issues a new request to the URL in the Location header. The server has no control over this request and every web browser makes a GET request.

Now, if you want to redirect a POST request to another URL and keep the same POST method, then you can use the 307 status code with a Location header and the browser should do the right thing. Note that this code does not let you change a GET into a POST - it just keeps the same method as the original request for the followup redirection request.

Community
  • 1
  • 1
Rob Allen
  • 12,643
  • 1
  • 40
  • 49