8

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);
}
Tri Nguyen
  • 93
  • 1
  • 3
  • 10

2 Answers2

6

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

a_myze
  • 91
  • 1
  • 3
0

Just guessing, but did you try without the "charset"? Like this:

.contentType("application/x-www-form-urlencoded")

Or

.contentType(ContentType.URLENC)
Arno
  • 305
  • 3
  • 14
  • There is no suggestion for APPLICATION_FORM_URLENCODED in ContentType at all – Tri Nguyen Mar 31 '17 at 09:40
  • There is, if you're importing "org.apache.http.entity.ContentType" – Arno Mar 31 '17 at 09:44
  • it isnt compatible with Rest Assured – Tri Nguyen Mar 31 '17 at 10:16
  • I'm sorry, you're right. For Rest assuerd it should be "ContentType.URLENC", but I don't know if that helps. As I don't know what parameters you're exactly sending in the body, I can't really test it myself. – Arno Mar 31 '17 at 10:34
  • This doesnot work , still getting java.lang.IllegalArgumentException: Cannot serialize because cannot determine how to serialize content-type application/x-www-form-urlencoded – Khyati Sehgal Aug 24 '17 at 10:21