Rest Assured - 无法序列化,因为无法确定如何序列化内容类型

时间:2017-03-31 08:27:53

标签: java restful-authentication 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);
}

2 个答案:

答案 0 :(得分:0)

只是猜测,但你没试过“charset”吗?像这样:

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

或者

.contentType(ContentType.URLENC)

答案 1 :(得分:0)

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

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

信用以下帖子:https://github.com/rest-assured/rest-assured/issues/841