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

8

我正在使用 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个回答

6

我遇到了同样的问题,并通过使用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

0

只是猜测,你有没有尝试过不带“charset”?像这样:

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

或者

.contentType(ContentType.URLENC)

ContentType中根本没有APPLICATION_FORM_URLENCODED的建议。 - Tri Nguyen
如果你正在导入"org.apache.http.entity.ContentType",那么就会有。 - Arno
它与Rest Assured不兼容。 - Tri Nguyen
对不起,你是正确的。对于Rest Assured应该是 "ContentType.URLENC",但我不知道这是否有帮助。由于我不知道您在正文中发送了哪些参数,因此我无法自行测试。 - Arno
这不起作用,仍然收到java.lang.IllegalArgumentException: 无法序列化,因为无法确定如何序列化内容类型application/x-www-form-urlencoded。 - Khyati Sehgal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接