Java 放心吧-可以';带参数和主体的t柱
我正在使用REST-Assured测试restapi。我在尝试发布url和正文内容中的参数时遇到错误。手动测试时,此功能正常工作。从url中删除参数不是一个选项 测试代码:Java 放心吧-可以';带参数和主体的t柱,java,rest,rest-assured,Java,Rest,Rest Assured,我正在使用REST-Assured测试restapi。我在尝试发布url和正文内容中的参数时遇到错误。手动测试时,此功能正常工作。从url中删除参数不是一个选项 测试代码: String endpoint = http://localhost:8080/x/y/z/id?custom=test; String body = "[{\"boolField\":true,\"intField\":991}, {\"boolField\":false,\"intFiel
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不允许在帖子中同时包含参数和正文内容 我不太熟悉rest assured,但您应该能够将这些参数移动到身体中。这就是典型的POST参数的工作方式。将参数作为请求URL的一部分通常仅用于GET。也许可以尝试将“custom=test”作为正文的第一行?您需要将参数指定为queryParameter,而不是“param”或“parameter”。POST参数将默认为在请求正文中发送的表单参数 即
必须将参数指定为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();
}Im使用的是非常旧的Rest Assured 1.1.6。然而,查看这段代码似乎仍然是一个问题,我不知道你可以有Post参数和一个body,所以可能Rest-Assured的创建者也没有。您是否尝试自己构建Rest Assured并注释此检查?为Rest Assured创建了一个问题:不幸的是,从URL中删除参数不是一个选项。此外,如果您使用的是1.1.6,我认为存在一个错误,您在查询URL中指定的参数被视为表单参数,而不是POST的查询参数。这个问题很久以前就解决了。你真的应该更新到新版本。
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();