0

I am trying to consume a third party REST API using Spring's RestTemplate component. I have tried entering the same request on an external REST API Client (Postman) - using the same URI and custom headers and I am able to retrieve the correct data.

However, when I tried to mirror the exact request using RestTemplate, it returns me

        <html><head>
        <title>302 Found</title>
        </head><body>
        <h1>Found</h1>
        <p>The document has moved <a href="https://address/{path of endpoint}">here</a>.</p>
        <hr>
        <address>Apache/2.4.7 (Ubuntu) Server at address Port 80</address>
        </body></html>

This is a sample of the code I am using:

        String uri = "http://address/{path of endpoint}";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set(someCustomHeaderKey, someCustomHeaderValue);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);

I have read that java does not allow redirect from one protocol to another, for instance, from http to https and vise versa. Would require some help on the approach on this.

Derrick Peh
  • 309
  • 2
  • 8
  • 20

2 Answers2

0

RestTemplate will follow redirects by default, but not if the protocol is different, which is the situation that you are seeing (redirecting from http to https).

For a more full explanation, and code that makes this work, see HTTPURLConnection Doesn't Follow Redirect from HTTP to HTTPS

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
0

I have tried your code on my local machine and everything seems fine. 302 status code is indicating that your URI location is different. as per your example, you should use https instead of Http in URI

I have tried your code as below

String uri = "https://jsonplaceholder.typicode.com/todos/1";
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("link", "http/:");
    HttpEntity<String> entity = new HttpEntity<String>(headers);
    ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, 
    entity, String.class);
    System.out.println(response);

output in console

<200,{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
},[Date:"Thu, 12 Mar 2020 03:45:44 GMT", Content-Type:"application/json; charset=utf-8", Content-Length:"83", Connection:"keep-alive", Set-Cookie:"__cfduid=d3104b8bbd25cbcb802977fc9183d559e1583984744; expires=Sat, 11-Apr-20 03:45:44 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax", X-Powered-By:"Express", Vary:"Origin, Accept-Encoding", Access-Control-Allow-Credentials:"true", Cache-Control:"max-age=14400", Pragma:"no-cache", Expires:"-1", X-Content-Type-Options:"nosniff", Etag:"W/"53-hfEnumeNh6YirfjyjaujcOPPT+s"", Via:"1.1 vegur", CF-Cache-Status:"HIT", Age:"1747", Accept-Ranges:"bytes", Expect-CT:"max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"", Server:"cloudflare", CF-RAY:"572a862eab83d5e8-BOM"]>
SSK
  • 3,444
  • 6
  • 32
  • 59