title: Spring RestTemplate exchange POST HttpClientException with any non 200 OK response
body: <p>So the current issue I am having is I am getting a</p>
  
  <blockquote>
    <p>Servlet.service() for servlet [dispatcherServlet] in context with path
    [] threw exception [Request processing failed; nested exception is
    org.springframework.web.client.HttpClientErrorException: 404 null]
    with root cause
    org.springframework.web.client.HttpClientErrorException 404 null</p>
  </blockquote>
  
  <p>in my client code when my server code responds with anything other than a OK 200 response. So in this case I was intentionally returning a 404 response in my server code having done so with a header and body, and without a header and body but I still get the same exception with the HTTP status code I respond with in the server code and null which in this case I assume to be the body of the response. Originally in my server code I always returned a <code>ResponseEntity&lt;&gt;("Sent", HttpStatus.OK)</code> but since in my server code I make a HTTP request elsewhere if this responds with anything other than a 200 OK then my client code wouldn't know about it therefore instead I am returning the actual response returned from the HTTP request in my server code back to my client code which is when I ran into this issue. </p>
  
  <p><strong>Client Code</strong></p>
  
  <pre><code>public String callFruitBasket(Fruit fruitRequest) {
      HttpHeaders requestHeaders = new HttpHeaders();
      requestHeaders.setContentType(MediaType.APPLICATION_JSON);
  
      HttpEntity&lt;Fruit&gt; fruit = new HttpEntity&lt;&gt;(fruitRequest, requestHeaders);
      System.out.println("Fruit headers: " + fruit.getHeaders());
  
      ResponseEntity&lt;String&gt; response = restTemplate.exchange(fruitBasketUrl, HttpMethod.POST, fruit, String.class);
      System.out.println("Full response: " + response);
  
      return response.getBody();
  }
  </code></pre>
  
  <p><strong>Server Code</strong></p>
  
  <pre><code>@PostMapping("/fruitBasket/send")
  public ResponseEntity&lt;String&gt; sendFruitBasket(@RequestBody Fruit fruit) {
     // works fine, old response
     // return new ResponseEntity&lt;&gt;("Sent", HttpStatus.OK);
  
     return new ResponseEntity&lt;&gt;("Baaad Request", HttpStatus.BAD_REQUEST);
  }
  </code></pre>
  
  <p>So at the moment in my server code I didn't add any headers to my response but I have already tried this with adding a Content-Type however I found I still got the same exception in my client code so I am 100% sure that the issue lies somewhere in my client code. Originally when I passed the 200 OK response the printout of the full response was fine in the client code with showing:</p>
  
  <pre><code>Full Response: &lt;200 OK,Sent,{Content-Type=[text/plain;charset=UTF-8], Content-Length=[4], Date=[Sat, 19 May 2018 09:10:21 GMT]}&gt;
  </code></pre>
  
  <p>What I would expect for any of the other Http Status code like 400 and 404 would be the same but with the 400 or 404 in the response instead of the 200 OK.
  I've tried playing around with the headers in both client and server code as I have read in various posts on here that this is commonly the cause when getting this type of exception which leads me to believe there could be something fundamental missing in my client code or that this is expected behavior for exchange() and that I am misunderstanding it.</p>