请放心 - 无法序列化,因为无法确定如何序列化内容类型

Tri*_*yen 6 java restful-authentication rest-assured

我正在使用Rest Assured 来测试API

当我发布身份验证请求时,出现错误:"java.lang.IllegalArgumentException: Cannot serialize because cannot determine how to serialize content-type application/x-www-form-urlencoded;charset=UTF-8"

这是我的测试方法

 @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);
}
Run Code Online (Sandbox Code Playgroud)

小智 6

我遇到了同样的问题,并通过使用formParams代替body解决了该问题。代码片段如下:

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

归功于以下帖子:https ://github.com/rest-assured/rest-assured/issues/841


Arn*_*rno 0

只是猜测,但是你尝试过不使用“字符集”吗?像这样:

.contentType("application/x-www-form-urlencoded")
Run Code Online (Sandbox Code Playgroud)

或者

.contentType(ContentType.URLENC)
Run Code Online (Sandbox Code Playgroud)