【问题标题】:Rest Assured - Cannot serialize because cannot determine how to serialize content-type放心 - 无法序列化,因为无法确定如何序列化内容类型
【发布时间】:2017-08-25 10:47:03
【问题描述】:

我正在使用 Rest Assured 测试 API

当我发布 authentication 请求时,出现的错误是:"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);
}

【问题讨论】:

    标签: java restful-authentication rest-assured


    【解决方案1】:

    我遇到了同样的问题,并通过使用 formParams 代替 body 解决了这个问题。代码如下:sn-p:

    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

    【讨论】:

      【解决方案2】:

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

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

      或者

      .contentType(ContentType.URLENC)
      

      【讨论】:

      • ContentType 中根本没有 APPLICATION_FORM_URLENCODED 的建议
      • 有,如果你正在导入“org.apache.http.entity.ContentType”
      • 它与 Rest Assured 不兼容
      • 对不起,你是对的。对于 Rest assuerd,它应该是“ContentType.URLENC”,但我不知道这是否有帮助。由于我不知道您在正文中确切发送了哪些参数,因此我自己无法真正进行测试。
      • 这不起作用,仍然出现 java.lang.IllegalArgumentException: Cannot serialize because cannot determine how to serialize content-type application/x-www-form-urlencoded
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多