-1

Could you please help me with the following: I have the following method:

public static CloseableHttpResponse getRequest (String url) {
    try (CloseableHttpClient httpClient = HttpClients.createDefault();){
        HttpGet httpget = new HttpGet(url); //http get request (create get connection with particular url)
        return httpClient.execute(httpget);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

Where I use CloseableHttpClient with try-with-resources I invoke method in some simple test:

CloseableHttpResponse closeableHttpResponse = RestClient.getRequest("https://reqres.in/api/users?page=2");
String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
JSONObject responseJson = new JSONObject(responseString);
System.out.println(responseJson);

And I am getting error: org.apache.http.TruncatedChunkException: Truncated chunk (expected size: 379; actual size: 358)

When I am not using try-with-resources like that:

public static CloseableHttpResponse getRequest (String url) throws IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(url); //http get request (create get connection with particular url)
    return httpClient.execute(httpget);
}

I have no error at all! Could you please explain - what the wrong? I am newbie and have no clue - some examples from internet are working good...

xtratic
  • 4,600
  • 2
  • 14
  • 32
  • 1
    Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the full source code you have as a [mcve], which can be compiled and tested by others. Please see: [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236). – Progman Feb 08 '21 at 19:39
  • The subsequent edits were really starting to deteriorate this question further and further... I've rolled back to when things made a little more sense. Seriously what the heck was going on? lol – xtratic Feb 08 '21 at 19:42

1 Answers1

1

The try-with-resources block will automatically call close() on the object, so the return from one of those getRequest calls is a closed CloseableHttpClient instance.

The call without try-with-resources will return a working (not closed) CloseableHttpClient. Put a really clear comment on the version which does not auto-close:

/**
  * http get request (create get connection with particular url)
  * @return open CloseableHttpResponse connection, caller must close after use.
  */
public static CloseableHttpResponse getRequest(String url) throws IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet(url);
    return httpClient.execute(httpget);
}

The callers need to handle close explicitly after use, or via try-with-resources like this:

try(CloseableHttpResponse response = getRequest(url)) {
    // Process response
}
DuncG
  • 12,137
  • 2
  • 21
  • 33
  • how do we close the connection in this case? i tried with finally block but it leads to truncated exception again. After removing the finally block the code works – Sanj. K Mar 07 '23 at 09:00
  • @Sanj.K See example above – DuncG Mar 09 '23 at 16:20