title: Rest-Assured/JUnit returns HTTP error code 400
body: <p>I wrote a simple REST in Spring boot and a test case written in rest assured returns http code 400 while it works fine from curl.</p>
  
  <p>Service:</p>
  
  <pre><code>@RestController
  @RequestMapping("/customerService")
  public class CustomerService
  {
      private static final Logger logger = Logger.getLogger(CustomerService.class.getName());
  
      @RequestMapping(value = "/customers/{id}", 
          produces = { "application/hal+json", "application/json" }, 
          consumes = { "application/json" }, method = RequestMethod.GET)
      public ResponseEntity&lt;Customer&gt; getCustomer(@PathVariable (value="id") String id)
      {
          logger.debug("getCustomer ["+id+"]");
  
          Customer customer = new Customer();
          ResponseEntity&lt;Customer&gt; response = ResponseEntity.ok(customer);
  
          customer.id="10";
          customer.name="Foobar";
          customer.category="Elite";
  
          return response;
      }
  }
  </code></pre>
  
  <p>Test Class:</p>
  
  <pre><code>import org.junit.FixMethodOrder;
  import org.junit.Test;
  import org.junit.runners.MethodSorters;
  
  import static com.jayway.restassured.RestAssured.given;
  import static org.junit.Assert.assertNotNull;
  
  import java.io.IOException;
  import java.nio.file.Files;
  import java.nio.file.Paths;
  
  @FixMethodOrder (MethodSorters.NAME_ASCENDING)
  public class TestCustomer 
  {
      @Test
      public void testStatus() throws IOException
      {
          String token = this.loadContentsFromFile("./src/test/resources/user-token.txt");
          String response = 
                  given()
                  .header("Authorization",token).and().header("Accept","application/json")
                  .contentType("application/json")
                  .when()
                      .get("http://localhost:8080/customerService/customers/10")
                     .peek() // Use peek() to print the ouput
                      .asString();
  
          assertNotNull(response);
          System.out.println("body : " +response);
      }   
      public String loadContentsFromFile(String path) throws IOException 
      {
          return new String(Files.readAllBytes(Paths.get(path)));
      }
  }
  </code></pre>
  
  <p>Test Runner:</p>
  
  <pre><code>public class TestSuite {
      public static void main(String[] args) {
          Result result = JUnitCore.runClasses(TestCustomerService.class);
  
          for (Failure failure : result.getFailures()) {
              System.out.println(failure.toString());
          }
  
          System.out.println(result.wasSuccessful());
      }
  }
  </code></pre>
  
  <p>Test Output:</p>
  
  <pre><code>customer.TestCustomer &gt; testStatus STANDARD_OUT
  DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Get connection for route {}-&gt;http://localhost:8080
  DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator - Connecting to localhost:8080
  DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: ignoreCookies
  DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
  DEBUG org.apache.http.client.protocol.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
  DEBUG org.apache.http.impl.client.DefaultHttpClient - Attempt 1 to execute request
  DEBUG org.apache.http.impl.conn.DefaultClientConnection - Sending request: GET /customerService/customers/10 HTTP/1.1
  DEBUG org.apache.http.wire -  &gt;&gt; "GET /customerService/customers/10 HTTP/1.1[\r][\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "Content-Type: application/json; charset=UTF-8[\r][\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ1c2VyIiwiYXVkaWVuY2UiOiJ3ZWIiLCJjcmVhdGVkIjoxNDc5MTM5ODg4NzM0LCJleHAiOjE0Nzk3NDQ2ODh9.YaaNqBXGo3M3fDI0HOL9eRH1G_w0iEZ4ZRxDPE004_QP59ieP20IJv3b1Y74R642yK9I2gjm-YoVvfmM3oqIEQ[\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "[\r][\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "Accept: application/json[\r][\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "Content-Length: 0[\r][\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "Host: localhost:8080[\r][\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "Connection: Keep-Alive[\r][\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_112)[\r][\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "Accept-Encoding: gzip,deflate[\r][\n]"
  DEBUG org.apache.http.wire -  &gt;&gt; "[\r][\n]"
  DEBUG org.apache.http.headers - &gt;&gt; GET /customerService/customers/10 HTTP/1.1
  DEBUG org.apache.http.headers - &gt;&gt; Content-Type: application/json; charset=UTF-8
  DEBUG org.apache.http.headers - &gt;&gt; Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ1c2VyIiwiYXVkaWVuY2UiOiJ3ZWIiLCJjcmVhdGVkIjoxNDc5MTM5ODg4NzM0LCJleHAiOjE0Nzk3NDQ2ODh9.YaaNqBXGo3M3fDI0HOL9eRH1G_w0iEZ4ZRxDPE004_QP59ieP20IJv3b1Y74R642yK9I2gjm-YoVvfmM3oqIEQ
  
  DEBUG org.apache.http.headers - &gt;&gt; Accept: application/json
  DEBUG org.apache.http.headers - &gt;&gt; Content-Length: 0
  DEBUG org.apache.http.headers - &gt;&gt; Host: localhost:8080
  DEBUG org.apache.http.headers - &gt;&gt; Connection: Keep-Alive
  DEBUG org.apache.http.headers - &gt;&gt; User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_112)
  DEBUG org.apache.http.headers - &gt;&gt; Accept-Encoding: gzip,deflate
  DEBUG org.apache.http.wire -  &lt;&lt; "HTTP/1.1 400 [\r][\n]"
  DEBUG org.apache.http.wire -  &lt;&lt; "Transfer-Encoding: chunked[\r][\n]"
  DEBUG org.apache.http.wire -  &lt;&lt; "Date: Tue, 15 Nov 2016 02:26:21 GMT[\r][\n]"
  DEBUG org.apache.http.wire -  &lt;&lt; "Connection: close[\r][\n]"
  DEBUG org.apache.http.wire -  &lt;&lt; "[\r][\n]"
  DEBUG org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 400
  DEBUG org.apache.http.headers - &lt;&lt; HTTP/1.1 400 
  DEBUG org.apache.http.headers - &lt;&lt; Transfer-Encoding: chunked
  DEBUG org.apache.http.headers - &lt;&lt; Date: Tue, 15 Nov 2016 02:26:21 GMT
  DEBUG org.apache.http.headers - &lt;&lt; Connection: close
  WARN com.jayway.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder - Could not parse content-type: Response does not have a content-type header
  DEBUG com.jayway.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder - Parsing response as: application/octet-stream
  DEBUG com.jayway.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder - Parsed data to instance of: class org.apache.http.conn.EofSensorInputStream
  DEBUG org.apache.http.wire -  &lt;&lt; "0[\r][\n]"
  DEBUG org.apache.http.wire -  &lt;&lt; "[\r][\n]"
  DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@32f271d9
  DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection 0.0.0.0:41492&lt;-&gt;127.0.0.1:8080 shut down
  HTTP/1.1 400 
  Transfer-Encoding: chunked
  Date: Tue, 15 Nov 2016 02:26:21 GMT
  Connection: close
  Executing test testStatus [customer.TestCustomer] with result: SUCCESS
  </code></pre>
  
  <p>Following line says its returning 400:</p>
  
  <p><strong>DEBUG org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 400</strong></p>
  
  <p>Here's the curl request which works fine:</p>
  
  <pre><code>curl -v --header 'Content-type: application/json' \
  --header 'Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1ZGllbmNlIjoid2ViIiwiY3JlYXRlZCI6MTQ3ODg4ODk0ODExNiwiZXhwIjoxNDc5NDkzNzQ4fQ.nA-5UY3L6HQP-TUjhYgMg1wcQa1Q1GQwPtGxbU3wgctO_c7vMmOd_hhG4Dj28x8dswivVBTCAXYJd1-37CQFfg' \
  --request GET http://localhost:8080/customerService/customers/10 \
  </code></pre>
  
  <p>Here's the curl response with returns code 200 and the body:</p>
  
  <pre><code>*   Trying 127.0.0.1...
  * Connected to localhost (127.0.0.1) port 8080 (#0)
  &gt; GET /customerService/customers/10 HTTP/1.1
  &gt; Host: localhost:8080
  &gt; User-Agent: curl/7.47.0
  &gt; Accept: */*
  &gt; Content-type: application/json; charset=UTF-8
  &gt; Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1ZGllbmNlIjoid2ViIiwiY3JlYXRlZCI6MTQ3ODg4ODk0ODExNiwiZXhwIjoxNDc5NDkzNzQ4fQ.nA-5UY3L6HQP-TUjhYgMg1wcQa1Q1GQwPtGxbU3wgctO_c7vMmOd_hhG4Dj28x8dswivVBTCAXYJd1-37CQFfg
  &gt; 
  &lt; HTTP/1.1 200 
  &lt; X-Content-Type-Options: nosniff
  &lt; X-XSS-Protection: 1; mode=block
  &lt; Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  &lt; Pragma: no-cache
  &lt; Expires: 0
  &lt; X-Frame-Options: DENY
  &lt; Content-Type: application/hal+json;charset=UTF-8
  &lt; Transfer-Encoding: chunked
  &lt; Date: Tue, 15 Nov 2016 02:47:53 GMT
  &lt; 
  {
    "id" : "10",
    "name" : "Foobar",
    "category" : "Elite"
  * Connection #0 to host localhost left intact
  }
  </code></pre>
  
  <p>So I am trying to figure out what am I doing wrong. One thing puzzles me why rest-assured is adding 'Content-length:0' header to the request. I am learning rest-assured and JUnit testing framework and I am stuck trying to get one GET request to work. Any help would be appreciated.</p>