【问题标题】:Rest Assured - can't POST with Parameters and Body放心 - 不能 POST 带有参数和正文
【发布时间】:2012-08-19 12:57:03
【问题描述】:

我正在使用 Rest Assured 测试 REST api。尝试使用 url 和正文内容中的参数进行 POST 时遇到错误。这在手动测试时可以正常工作。从 url 中删除参数不是一个选项

测试代码:

String endpoint = http://localhost:8080/x/y/z/id?custom=test;
String body = "[{\"boolField\":true,\"intField\":991},
                {\"boolField\":false,\"intField\":998}]";
expect().spec(OK).given().body(body).post(endpoint);

运行时会抛出以下错误

You can either send parameters OR body content in the POST, not both!

java.lang.IllegalStateException: You can either send parameters OR body content in the POST, not both!
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:198)
at com.jayway.restassured.internal.RequestSpecificationImpl.sendRequest(RequestSpecificationImpl.groovy:282)
at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$sendRequest(RequestSpecificationImpl.groovy)
at com.jayway.restassured.internal.RequestSpecificationImpl$this$2$sendRequest.callCurrent(Unknown Source)
at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy:83)
...

为什么 Rest Assured 不允许在 POST 中同时使用参数和正文内容?

【问题讨论】:

  • 我使用的是比较老的 Rest Assured 1.1.6。但是,查看github 上的代码,这似乎仍然是个问题
  • 我不知道你可以有 Post 参数和 body,所以 Rest Assured 的创建者可能也没有。您是否尝试过自己构建 Rest Assured 并注释掉此检查?

标签: java rest rest-assured


【解决方案1】:

在我看来没有通过

.header("Content-Type", "application/json") 

在正文之后,您的调用将产生状态码 415

【讨论】:

    【解决方案2】:

    您必须将参数指定为 queryParam。示例如下:

    RequestSpecification request=new RequestSpecBuilder().build();
    ResponseSpecification response=new ResponseSpecBuilder().build();
    @Test
    public void test(){
      User user=new User();
      given()
      .spec(request)
      .queryParam(query_param1_name, query_param1_name_value)
      .queryParam(query_param2_name, query_param2_name_value)
      .contentType(ContentType.JSON)
      .body(user)
      .post(API_ENDPOINT)
      .then()
      .statusCode(200).log().all();
    

    }

    【讨论】:

      【解决方案3】:

      您需要将参数指定为 queryParameter 而不是“param”或​​“parameter”。 POST 的参数将默认为在请求正文中发送的表单参数。

      given().
              queryParam("name, "value").
              body(..).
      when().
              post(..);
      

      【讨论】:

      • 另外,如果您使用的是 1.1.6,我认为您在查询 URL 中指定的参数被视为表单参数而不是 POST 的查询参数存在错误。这是很久以前修复的。你真的应该更新到新版本。
      【解决方案4】:

      我对rest-assured不太熟悉,但是你应该可以将这些参数移动到body上。这就是典型的 POST 参数的工作方式。将参数作为请求 URL 的一部分通常仅用于 GET。也许尝试将“custom=test”作为正文的第一行?

      【讨论】:

      • 很遗憾,不能从 URL 中删除参数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 2021-08-17
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多