routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

Set HttpCode other than default 302 on @Redirect

Open ETNOL opened this issue 6 years ago • 4 comments

Is it possible to set a different status code other than 302 when using the @Redirect decorator? Neither @HttpCode nor response.status(301) seem to work.

ETNOL avatar Mar 01 '18 21:03 ETNOL

I ended up not using a decorator for redirection, but grabbing response using @Res, and then calling redirect on that:

response.redirect(303, 'https://example.com');
return response;

madve2 avatar Nov 26 '18 12:11 madve2

If I use

response.redirect(301, "https://example.com");

then, any async interceptor doesn't seem to work correctly. It would be better idea to add support to set status code to @Redirect decorator.

scvgoe avatar Jul 31 '19 02:07 scvgoe

Similar problem here. I am generating an AWS pre-signed url. Passing it with @Redirect(':url') does not work, as the redirect seems to encode the query parameters.

Using return response.redirect(url) works on the client side, but on the server it activates the 404 middleware and Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client.

Anything that I can do about this? Thanks in advance!

sheinzle avatar Jan 17 '20 19:01 sheinzle

Hi @sheinzle That's a common issue happens when you controller returns undefined and you error handler return by default an 404 error, you can add the next lines of code on you error handler

if (res.headersSent) {
  return;
}

Hope this help.

MaikMike avatar Apr 09 '20 19:04 MaikMike