14

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 でパラメータと本文の両方を許可しないのはなぜですか?

4

4 に答える 4

35

パラメータは、「param」や「parameter」ではなく、queryParameter として指定する必要があります。POST の Param は、リクエスト本文で送信されるフォーム パラメータにデフォルト設定されます。

いえ

given().
        queryParam("name, "value").
        body(..).
when().
        post(..);
于 2012-08-28T06:26:49.537 に答える
0

私は安心感にあまり慣れていませんが、これらのパラメーターを体に移動できるはずです。これが典型的なPOSTパラメータの仕組みです。リクエストURLの一部としてパラメータを設定することは、通常、GETに対してのみ行われます。たぶん、「custom =test」を本文の最初の行にしてみてください。

于 2012-08-23T23:25:42.673 に答える