Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect to a url from a micronaut controller?

I have a controller which accepts some params process it and should return a param back to UI by redirecting to a given url.

I have tried HttpResponse.seeOther and HttpResponse.redirect both displays bank page and "see other" message but does not redirect to an existing servable page. What am I missing here?

@Post('/getServ')
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
HttpResponse<String> serve(@Nullable @Body LinkedHashMap payload) {
    //...
    URI location = new URI(redirectURL)
        return HttpResponse.redirect(location).
            header("Location", redirectURL).
            status(302).
            header("JWT", jwt.toString())
    //...
}

The expected result should be a redirection to a url from the controller.

like image 728
Ricky Avatar asked Oct 29 '25 14:10

Ricky


1 Answers

Have not tried it, but this should work:

@Post('/getServ')
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
HttpResponse<String> serve(@Nullable @Body LinkedHashMap payload) {
    //...
    URI location = new URI(redirectURL)
        return HttpResponse.redirect(location).
            header("JWT", jwt.toString())
    //...
}

Your endpoint does not produce text/html - the redirect is an empty response with just the Location header set (by the framework). However, returning that (Content-Type) header may confuse your browser.

You should also not need to additionally set the status code and the location header directly.

like image 107
lena_punkt Avatar answered Oct 31 '25 06:10

lena_punkt