-2

I am attempting a GET request to another API to get a json response.

When I make the request with the code below, I am getting HTTP 504 Error (Gateway timeout error).

However, when I tried it through rest client tool, the request does not throw any error.

How do I increase the time gap in my code to avoid the timeout error?

This is how the call looks like:

HttpClient httpClient = getBasicAuthDefaultHttpClient();
String url= "http://XXXXX";
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("id", id);
httpGet.addHeader("secret", secret);
httpGet.addHeader("network_val", networkval);

HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();

if (entity != null && response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
    ObjectMapper objectMapper = new ObjectMapper();
    restInfo = objectMapper.readValue(entity.getContent(), MyClass.class);
} else {
    logger.error("Call to API failed: response code = {}", response.getStatusLine().getStatusCode());
}

Please note:

Could this be something to do with 'https'?

When I try with 'http' through my Insomnia REST Client I get the ERROR: error: Failure when receiving data from the peer. https works fine without any error (https://XXXXX)

hooknc
  • 4,854
  • 5
  • 31
  • 60
user3919727
  • 283
  • 2
  • 7
  • 25
  • Please post a [MCVE](https://stackoverflow.com/help/minimal-reproducible-example). In this code you aren't even executing your Get request. – xtratic Oct 14 '19 at 18:31
  • I just updated. – user3919727 Oct 14 '19 at 18:39
  • 2
    The endpoint may not be listening on the standard `http` port and may only be serving over `https` so yes, try with `https`. Other than that, without knowing much about the server, there's little we can do to help. – xtratic Oct 14 '19 at 18:44

1 Answers1

1

This is what I tried.

public HttpClient getBasicAuthDefaultHttpClient() {
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, 
    password);
    provider.setCredentials(AuthScope.ANY, creds);

    //Fix to avoid HTTP 504 ERROR (GATEWAY TIME OUT ERROR)
    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    requestBuilder.setConnectTimeout(30 * 1000);
    requestBuilder.setConnectionRequestTimeout(30 * 1000);

    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setDefaultRequestConfig(requestBuilder.build());
    builder.setDefaultCredentialsProvider(provider).build();

    return builder.build();
}
user3919727
  • 283
  • 2
  • 7
  • 25