Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading