title: Why my custom ClientHttpRequestInterceptor with empty response
body: <p>I have done the following for my custom logging interceptor</p>
  
  <pre><code>public class HttpLoggingInterceptor implements ClientHttpRequestInterceptor {
      private final static Logger log = LoggerFactory.getLogger(HttpLoggingInterceptor.class);
  
      @Override
      public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
          logRequest(request, body);
          ClientHttpResponse response = execution.execute(request, body);
          logResponse(response);
          return response;
      }
  
      private void logRequest(HttpRequest request, byte[] body) throws IOException {
          log.info("Request URI : {}, Method : {}, Headers : {}, Request body : {}", request.getURI(), request.getMethod(), request.getHeaders(), new String(body, "UTF-8"));
  
      }
  
      private void logResponse(ClientHttpResponse response) throws IOException {
          log.info("Response Status code : {}, Status text : {}, Headers : {}, Response body: {}", response.getStatusCode(), response.getStatusText(), response.getHeaders(), StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
      }
  }
  </code></pre>
  
  <p>And I am setting the intercepter to the restTemplate </p>
  
  <pre><code>   @Autowired
      public RestTemplate restTemplate;
  
      @Override
      public void onApplicationEvent(ApplicationReadyEvent event) {
          List&lt;ClientHttpRequestInterceptor&gt; clientHttpRequestInterceptors = new ArrayList&lt;&gt;();
          clientHttpRequestInterceptors.add(new HttpLoggingInterceptor());
  //        clientHttpRequestInterceptors.addAll(restTemplate.getInterceptors());
          restTemplate.setInterceptors(clientHttpRequestInterceptors);
  //        restTemplate.setInterceptors(Collections.singletonList(new HttpLoggingInterceptor()));
  }
  </code></pre>
  
  <p>The logger is printing the response properly to the console, But at the end the response is returned as empty to the caller. I am not able to debug and figure it out.</p>
  
  <p>I have figured it out that the <code>StreamUtils.copyToString(response.getBody(), Charset.defaultCharset())</code> is reading the input stream once and it is no more holding the response body in it (which is empty now)</p>
  
  <p>Anyone else also facing the same issue and has any idea of duplicating the InputStream without reading it from the original InputStream?</p>