Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rest Assured - Cannot serialize because cannot determine how to serialize content-type

I am using Rest Assured for testing API

WHEN I post a request for authentication, the error occurs to say that: "java.lang.IllegalArgumentException: Cannot serialize because cannot determine how to serialize content-type application/x-www-form-urlencoded;charset=UTF-8"

Here is my test method

 @Test
public void authenticate()
{
    AuthenDto authenDto = new AuthenDto("username","password","false","Login");
    given()
            .contentType("application/x-www-form-urlencoded;charset=UTF-8")
            .accept("application/json, text/plain, */*")
            .body(authenDto)
    .when()
            .post("ENDPOINT")
    .then()
            .statusCode(200);
}
like image 672
Tri Nguyen Avatar asked Mar 31 '17 08:03

Tri Nguyen


1 Answers

I faced the same problem and resolved the issue by using formParams in place of body. Code snippet below:

given().
contentType("application/x-www-form-urlencoded").
accept("*/*").
formParams(bodyContent).relaxedHTTPSValidation(). //bodyContent=hashMap variable
when().
post("/register").
then().
extract().
response();

Credit to following post: https://github.com/rest-assured/rest-assured/issues/841

like image 129
a_myze Avatar answered Nov 13 '22 03:11

a_myze