3

I have problem with making POST request with rest-assured. @Test public void deleteBook(){

    //Given
    
    Response response = given().
        contentType("application/json").
        body(" { \"Title\": \"Libro2\"}").
    when().
        post("api/books/").andReturn();
    
    int id = from(response.getBody().asString()).get("id");
        
    //When
    when().
        delete("api/books/{id}",id).
    //Then  
    then().
        statusCode(200).
        body("id", equalTo(id));
    
    when()
        .get("api/books/{id}",id).
    then()
        .statusCode(404);
}
java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but no content-type was defined in the response.
Try registering a default parser using:
   RestAssured.defaultParser(<parser type>);

I have run out of ideas whats wrong.

Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47
DIP
  • 31
  • 1
  • 3

2 Answers2

0

Try this:

Response response = given()
      .setAccept(ContentType.JSON)
      .setContentType(ContentType.JSON)
      .body(" { \"Title\": \"Libro2\"}")
      .when()
      .post("api/books/").andReturn();  
Villa_7
  • 508
  • 4
  • 14
0

For me helps using .setDefaultParser(Parser.JSON) in response specification

public static ResponseSpecification responseSpecOK200ForGetMethod(){
        return new ResponseSpecBuilder()
                .expectStatusCode(200)
                .setDefaultParser(Parser.JSON)
                .build();
}
Tushar
  • 3,527
  • 9
  • 27
  • 49
Andriy
  • 1