javaspringresttemplate

Imitate request from Postman with RestTemplate fails


I'm trying to consume a json from https://ws-public.interpol.int/notices/v1/red. The request with postman works perfectly:

Postman request

But when I try with Spring's RestTemplate as follows (trying to copy the request headers exactly):

final HttpHeaders headers = new HttpHeaders();

headers.set(HttpHeaders.CACHE_CONTROL, "no-cache");
headers.set(HttpHeaders.USER_AGENT, "PostmanRuntime/7.42.0");
headers.set(HttpHeaders.HOST, "ws-public.interpol.int");
headers.set(HttpHeaders.ACCEPT, "*/*");
headers.set(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, br");
headers.set(HttpHeaders.CONNECTION, "keep-alive");

final HttpEntity<Void> entity = new HttpEntity<>((Void) null, headers);

final RestTemplate restTemplate = new RestTemplateBuilder()
    .setConnectTimeout(Duration.ofMillis(5000L))
    .setReadTimeout(Duration.ofMillis(5000L))
    .build();

final String response = restTemplate
    .exchange(URL, HttpMethod.GET, entity, String.class)
    .getBody();

my request fails with 403 Forbidden:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: "<HTML><HEAD><LF><TITLE>Access Denied</TITLE><LF></HEAD><BODY><LF><H1>Access Denied</H1><LF> <LF>You don't have permission to access "http&#58;&#47;&#47;ws&#45;public&#46;interpol&#46;int&#47;" on this server.<P><LF>Reference&#32;&#35;18&#46;1cf95568&#46;1730991641&#46;2abc645d<LF><P>https&#58;&#47;&#47;errors&#46;edgesuite&#46;net&#47;18&#46;1cf95568&#46;1730991641&#46;2abc645d</P><LF></BODY><LF></HTML><LF>"
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:109)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:168)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:819)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:777)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:602)
    at com.company.sample.MySampleApp.main(MySampleApp.java:55)

How can I successfully imitate the request by Postman? What am I missing?


Solution

  • Edit:

    Upon further investigation, it seems it just wants the Cookie header to work in general so here is the new working example:

            RestTemplate restTemplate = new RestTemplateBuilder()
                    .defaultHeader(HttpHeaders.USER_AGENT, "PostmanRuntime/7.26.8")
                    .defaultHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, br")
                    .defaultHeader(HttpHeaders.COOKIE, "")
                    .build();
    
            final String response = restTemplate
                    .getForObject("https://ws-public.interpol.int/notices/v1/red", String.class);