2

I can't get my RestTemplate GET-Call to automatically follow a 308 PERMANENT_REDIRECT response.

I have a REST service that previously was reachable under the URL https://subdomain1.domain.com/x/y/z (just an example). It now moved to https://subdomain2.domain.com/a/y/z (change in the subdomain name and the first part of the path). When requesting the old URL, the server responds with the following response:

Status code  : 308 PERMANENT_REDIRECT
Status text  : Permanent Redirect
Headers      : {Server=[openresty/1.15.8.1], Date=[Wed, 18 Sep 2019 18:22:08 GMT], Content-Type=[text/html], Content-Length=[177], Connection=[keep-alive], Location=[https://subdomain2.domain.com/a/y/z], Access-Control-Allow-Headers=[Accept, Accept-Encoding, Authorization, Content-Length, Content-Type, Host, Origin, X-Requested-With, User-Agent, No-Auth-Challenge], Access-Control-Expose-Headers=[Location, ETag, Auth-Token, Auth-Token-Valid-Until, Auth-Token-Location, X-Powered-By], Access-Control-Allow-Origin=[*], Access-Control-Allow-Credentials=[true], Access-Control-Allow-Methods=[GET, OPTIONS, PUT, PATCH, POST, DELETE]}
Response body: <html>
<head><title>308 Permanent Redirect</title></head>
<body>
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>openresty/1.15.8.1</center>
</body>
</html>

I tried the following code lines:

RestTemplate restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory());

ResponseEntity<String> response = restTemplate.exchange("https://subdomain1.domain.com/x/y/z", HttpMethod.GET, null, String.class);

System.out.println("Response code: " + response.getStatusCode());
System.out.println(response.getBody());

The output is:

Response code: 308 PERMANENT_REDIRECT
<html>
<head><title>308 Permanent Redirect</title></head>
<body>
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>openresty/1.15.8.1</center>
</body>
</html>

So the redirect hasn't been followed.

I also tried the following (different request factory):

RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());

ResponseEntity<String> response = restTemplate.exchange("https://subdomain1.domain.com/x/y/z", HttpMethod.GET, null, String.class);

System.out.println("Response code: " + response.getStatusCode());
System.out.println(response.getBody());

And also (from another stackoverflow thread):

HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setHttpClient(HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build());
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);

ResponseEntity<String> response = restTemplate.exchange("https://subdomain1.domain.com/x/y/z", HttpMethod.GET, null, String.class);

System.out.println("Response code: " + response.getStatusCode());
System.out.println(response.getBody());

But the result is always the same.

Any ideas on how to get the request to follow the 308 permanent redirect?

  • I could not find the answer to this question but in my case it turned out I was calling http instead of https. See how the location differs from your request, maybe it's something you can solve without a redirect. – Cristian Stancalau Nov 04 '20 at 15:14

0 Answers0