title: ClientHttpResponse.getBody() throws ResourceAccessException when response code is 401
body: <p>I am trying to log the request-response pairs for every request. The problem is that when the response code is <code>401</code>, <code>ClientHttpResponse.getBody()</code> throws a ResourceAccessException and i cannot read the response body.  </p>
  
  <p>This is the RestTemplate configuration  </p>
  
  <pre><code>RestTemplate restTemplate = new RestTemplate();
  
  // Added this requestFactory to make response object readable more than once.
  ClientHttpRequestFactory requestFactory =
          new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
  restTemplate.setRequestFactory(requestFactory);
  restTemplate.getInterceptors().add(new RequestLoggingInterceptor(vCloudRequest.getAction(),httpHeaders));
      restTemplate.setErrorHandler(new RequestErrorHandler());
  
  return restTemplate;
  </code></pre>
  
  <p>The last line of the interceptor below throws the following exception.</p>
  
  <p>How can i resolve this problem?</p>
  
  <blockquote>
    <p>org.springframework.web.client.ResourceAccessException: I/O error on
    POST request for "<a href="https://example.com/api/sessions" rel="noreferrer">https://example.com/api/sessions</a>": Server returned
    HTTP response code: 401 for URL: <a href="https://example.com/api/sessions" rel="noreferrer">https://example.com/api/sessions</a>;
    nested exception is java.io.IOException: Server returned HTTP response
    code: 401 for URL: <a href="https://example.com.11/api/sessions" rel="noreferrer">https://example.com.11/api/sessions</a></p>
  </blockquote>
  
  <p>This is the related part of the interceptor.</p>
  
  <pre><code>@Override
  public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
      ClientHttpResponse response = execution.execute(request, body);
  
      String requestString = new String(body);
  
      // Calling this method because when we make a POST or PUT request and get an error
      // response.getBody() throws IOException. But if we call response.getStatusCode() it works fine.
      // I don't know the reason.
      // I asked a question on stackoverflow
      // https://stackoverflow.com/questions/47429978/resttemplate-response-getbody-throws-exception-on-4-and-5-errors-for-put-and
      response.getStatusCode();
      String responseString = new String(ByteStreams.toByteArray(response.getBody()), Charset.forName("UTF-8"));
  ...
  }
  </code></pre>
  
  <p>This is the custom error handler</p>
  
  <p>public class RequestErrorHandler implements ResponseErrorHandler {</p>
  
  <pre><code>@Override
  public boolean hasError(ClientHttpResponse response) throws IOException {
          if (!response.getStatusCode().is2xxSuccessful()) {
          return true;
      }
      return false;
  }
  
  @Override
  public void handleError(ClientHttpResponse response) throws IOException {
      JAXBElement&lt;ErrorType&gt; root = null;
  
      try {
          JAXBContext jaxbContext = JAXBContext.newInstance(ErrorType.class);
          Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  
          root = jaxbUnmarshaller.unmarshal(new StreamSource(
                  response.getBody()), ErrorType.class);
      } catch (JAXBException e) {
          throw new IOException("XML converting error. Cannot convert response to ErrorType");
      }
  
      ErrorType error = root.getValue();
      throw new VcloudException(error);
  }
  }
  </code></pre>