java - 请放心 - 无法使用参数和正文进行 POST

标签 java rest rest-assured

我正在使用 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 中同时包含参数和正文内容?

最佳答案

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

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

关于java - 请放心 - 无法使用参数和正文进行 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12101297/

相关文章:

java - 响应内容类型为 'null' 。放心异常

java - SpringBoot & 嵌入式Tomcat : Unpack executable war on server

java - 无法在Intellij Idea的Springboot项目上通过模板创建jsp文件

用于自动回归 (AR)、ARIMA、时间序列分析的 Java API

c# - 如何在没有身份验证和授权方法的情况下保护公共(public)端点

windows - 如何使用 REST+cURL 更新 TeamCity 构建参数

java - 放心 - post() - java.lang.NullPointerException : Cannot invoke method trim() on null object

java - 我如何在我的 JSP 页面中格式化这个代表十进制数字的字符串?

ios - 如何在 swift iOS 中通过 nsurlsession 为 REST API 创建全局函数?

rest-assured - 为什么 Hamcrest 的 containsInAnyOrder 匹配器接受数组而不是列表?