Skip to content
Advertisement

Rest Assured – can’t POST with Parameters and Body

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?

Advertisement

Answer

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(..);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement