I am working on a Spring boot project that uses Spring Webclient to do a GET request on a URL which has a 3xx redirection. Using webclient to do the GET request to that url gives 200 response. I would like to stop at the redirection and get a header named "location" from there.
I tried the following code and I am getting a null in response.
WebClient webclient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(
HttpClient.create().followRedirect(false)
)).build();
@GetMapping("/")
Mono<Object> webclientTest2() throws URISyntaxException {
return webclient.get()
.uri("https://URL_THAT_HAS_REDIRECTION")
.exchange()
.flatMap(res -> {
Headers headers = res.headers();
System.out.println("Status code: "+res.statusCode());
headers.asHttpHeaders().forEach((k,v) -> {
System.out.println(k+" : "+v);
});
return Mono.just(headers.asHttpHeaders().get("location"));
});
}
Webclient isnt stopping at 3xx redirection. The System.out.println statement shows only Status code: 200
and some headers from the next System.out.println statement. How can I get that location header from that 3xx redirection?
Note: One interesting observation I made is using the httpclient instead of webclient is able to retrieve the location header.
HttpClient httpclient = HttpClient.create().followRedirect(false);