14

I'm testing a REST api using Rest Assured. I'm running into an error when trying to POST with both a parameter in the url and body content. This works correctly when testing manually. Removing the parameter form the url is not an option

Test Code:

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);

Which throws the following error when run

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)
...

Why does Rest Assured not allow both parameters and body content in a POST?

Jake Walsh
  • 3,669
  • 5
  • 22
  • 28
  • Im using Rest Assured 1.1.6, which is rather old. However, looking at the code on [github](https://github.com/jayway/rest-assured/blob/master/rest-assured/src/main/groovy/com/jayway/restassured/internal/RequestSpecificationImpl.groovy) this still appears to be a problem – Jake Walsh Aug 23 '12 at 23:19
  • I didn't know you could have Post parameters and a body so maybe the creators of Rest Assured didn't either. Did you try building Rest Assured yourself and commenting out this check? – mercutio Aug 24 '12 at 00:06
  • created a issue for rest-assured: http://code.google.com/p/rest-assured/issues/detail?id=196&thanks=196&ts=1346105863 – Jake Walsh Aug 27 '12 at 22:17

4 Answers4

36

You need to specify the parameter as queryParameter and not "param" or "parameter". Param for POST will default to form parameters which are sent in the request body.

I.e.

given().
        queryParam("name, "value").
        body(..).
when().
        post(..);
Johan
  • 37,479
  • 32
  • 149
  • 237
  • Also if you're using 1.1.6 I think there was a bug where the parameters you specified in the query URL were treated as form parameters and not query parameters for POST. This was fixed a long time ago. You really should update to newer version. – Johan Aug 28 '12 at 06:29
0

I'm not too familiar with rest-assured, but you should be able to move those parameters to the body. That's how typical POST parameters work. Having parameters as part of the request URL is typically only done for GET. Maybe try making "custom=test" the first line of the body?

Joe K
  • 18,204
  • 2
  • 36
  • 58
0

You have to specify parameters as queryParam. Here is the example:

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();

}

Nitin Pawar
  • 1,634
  • 19
  • 14
0

in my opinion without passing

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

after body, your call will produce status code 415