Magento2: You cannot set a 303 redirect response using a result factory

Created on 27 Mar 2017  路  11Comments  路  Source: magento/magento2

Preconditions

  1. Tested in Magento EE 2.1.4
  2. Chrome on Mac OS

Steps to reproduce

  1. Perform a 303 redirect from any controller (see code below)
  2. Send a request to that controller endpoint
  3. Examine the response code

Expected result

  1. A 303 response code should be returned

Actual result

  1. A 302 response code is used instead

Example code (use in any controller extending the default Action):

$result = $this->resultRedirectFactory->create();
$result->setHttpResponseCode(303);

return $result->setPath('checkout/cart');
Fixed in 2.2.x Fixed in 2.3.x Clear Description Confirmed Format is valid Ready for Work Reproduced on 2.1.x Reproduced on 2.2.x Reproduced on 2.3.x up for grabs

Most helpful comment

Why do we need a 303 status code? Surely that question answers itself.

Use case:

  • I want to return a 303 response.

All 11 comments

Why need this? Please describe a complete use case.

Why do we need a 303 status code? Surely that question answers itself.

Use case:

  • I want to return a 303 response.

This is not a use case. For which purpose do you need such a status code?

There are a lot of codes with some $statusCode = $isPermanent ? 301 : 302 logic. To change this there should _really_ be a reason.

What you are asking is "why do you want to set a 303 response code?" which falls outside the remit of discussion in my opinion. Magento should be flexible enough to allow a custom response code, especially when the public API ($result->setHttpResponseCode()) implies that is allowed.

If you must know the use case, I am using XHR to send a PUT request, and then I want to send a 303 response code so it follows the redirect using GET instead.

Thanks for your input,

I am using XHR to send a PUT request, and then I want to send a 303 response code so it follows the redirect using GET instead

That was exactly what I was asking for.

Magento should be flexible enough to allow a custom response code

Any additional flexibility means more maintenance efforts. But of course ANY limitation/unimplemented feature should be reasonable.

After getting familiar with implementation it looks like https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Controller/ResultInterface.php#L13 is promising what you expect :)

In \Magento\Framework\Controller\AbstractResult::renderResult response code is set correctly first:

if (!empty($this->httpResponseCode)) {
    $response->setHttpResponseCode($this->httpResponseCode);
}

but then suddenly rewritten in render:

$response->setRedirect($this->url);

Fix seems to be as simple as

if (!empty($this->httpResponseCode)) {
    $response->setRedirect($this->url, $this->httpResponseCode);
} else {
    $response->setRedirect($this->url);
}

Anybody want to create a PR? \Magento\Framework\Controller\Test\Unit\Result\RedirectTest needs to be expanded of course.

@jameshalsall, thank you for your report.
We've created internal ticket(s) MAGETWO-80996 to track progress on the issue.

I'm working on it #SQUASHTOBERFEST

Internal ticket to track issue progress: MAGETWO-81973

The issue has been fixed in 2.2-develop branch and will be available with 2.2.2 release

Hi @jameshalsall. Thank you for your report.
The issue has been fixed in magento-engcom/magento2ce#1286 by @magento-engcom-team in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming patch release.

Was this page helpful?
0 / 5 - 0 ratings