1

A client that follows redirects can be created as follows:

WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(
                        HttpClient.create().followRedirect(true)
                ))

After invoking a HEAD request on a URL, how can the final Location header be retrieved? In other words, how can we get the final URL redirected to?

1 Answer 1

4

It is true that HttpClient#followRedirect(true) enables the redirection. However there is also HttpClient#followRedirect(BiPredicate<HttpClientRequest,HttpClientResponse>), here you can control more precisely when you want to redirect and in addition to this you have always access to the response and the Location header, so in any time you will know to which location there will be a redirection. More info here and here

For example

        WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(
                        HttpClient.create().followRedirect((req, res) -> {
                            System.out.println(res.responseHeaders().get("Location"));
                            return HttpResponseStatus.FOUND.equals(res.status());
                        })
                ))
Sign up to request clarification or add additional context in comments.

2 Comments

I came across this specific method but wasn't sure if the predicate gets called after every HTTP redirect, or just after the first HTTP call and then puts it on "autopilot" for the rest of the redirection chain.
After every redirect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.