title: Handle HttpClientErrorException$BadRequest: 400 Bad Request in Cucumber RestTemplate
body: <p>Hi I have written a Cucumber test case where i send a POST request with an XML body, output of that request is 400 Error with an XML body, which is expected and when i throw the request i get that too, but what i get is below:</p>
  
  <pre><code>org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request
      at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79)
      at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:97)
      at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)
      at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
      at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:777)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:709)
      at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:462)
      at uk.co.argos.services.order.StepDefinations.TestMethods.POSTrestTemplatewithXML(TestMethods.java:147)
      at uk.co.argos.services.order.StepDefinations.StepDefs.user_hits_the_getSlot_request_with_OrderEnricher_with_and_and(StepDefs.java:118)
      at ✽.Given User hits the getSlot request with OrderEnricher with "2020-40-32" and "150" and "MK92NW"(OrderEnricher_Negative.feature:5)
  </code></pre>
  
  <p>And my test step failes with bad request, But ideally that is my expected and i want to pass my test step &amp; scenario, not sure how should i handle it, i have tried applying multiple things. Can anyone help please?</p>
  
  <pre><code> public static ResponseEntity&lt;String&gt; POSTrestTemplatewithXML(URI uri, String XMLforPOST){
          ResponseEntity&lt;String&gt; responseEntity = null;
          try {
              RestTemplate restTemplate=new RestTemplate();
              List&lt;HttpMessageConverter&lt;?&gt;&gt; messageConverters=new ArrayList&lt;HttpMessageConverter&lt;?&gt;&gt;();
              messageConverters.add(new StringHttpMessageConverter());
              restTemplate.setMessageConverters(messageConverters);
              HttpHeaders headers=new HttpHeaders();
              headers.setContentType(MediaType.APPLICATION_XML);
              HttpEntity&lt;String&gt; request=new HttpEntity&lt;String&gt;(XMLforPOST, headers);
              responseEntity=restTemplate.postForEntity(uri, request, String.class);
  
  
  
          }
          catch (Exception e){
              System.out.println("RESPONSE-"  +responseEntity);
              byte[] bytes = ((HttpClientErrorException.BadRequest)e).getResponseBodyAsByteArray();
              assertTrue(true);
  
  //Convert byte[] to String
              String s = new String(bytes);
  
              System.out.println(s);
              e.printStackTrace();
          }
          return responseEntity;
      }
  </code></pre>