0

I have used CloseableHttpResponse and CloseableHttpClient to make API calls. CloseableHttpResponse response = httpClient.execute(httpRequest);

In one method I am reading the contents of the response as: String eventJson = IOUtils.toString(response.getEntity().getContent(), Charset.defaultCharset()); Also, I want to return this response and again read data in another method.

I will have to return response object due to my requirements and I cannot return stream or string. Again I need to read the content twice in two separate methods.

I already tried storing the contents in byte array and stuffs, but the issue is we cannot read the content from response twice.

I searched about some wrapper class and how can I clone the response object. I even tried to implement my own class for cloning HttpResponse but that doesn't seem to be possible.

  • forget it. you should NOT think of such a destructive way like cloning a `read-once` resource. it would be much more productive to rework your code and actually read it only once. – ursa Apr 12 '23 at 15:05

0 Answers0