简体   繁体   中英

Sending one item list as formParam with RestAssured

I'm trying to submit some JSON via POST to an endpoint with RestAssured. The API is expecting a list for a key. The value I need to send for that is a list with a single item.

The ideal request would look like:

Request method: POST
Request path:   http://localhost:8080/registrionURL
Request params: <none>
Query params:   <none>
Form params:    paramKey=[param space]
Path params:    <none>
Multiparts:     <none>

If I add the param as a list or a map it still just sends the parameter as a string:

    String paramKey = "paramKey";
    String paramWithSpace = "param space";
    Response response = RestAssured.given()
            .formParam(paramKey, new String[]{paramWithSpace})
            .and().contentType("application/json")
            .log().method().log().path().log().parameters()
            .when().post(MessageFormat.format(registrationUrl, testName));

Output:

Request method: POST
Request path:   http://localhost:8080/registrionURL
Request params: <none>
Query params:   <none>
Form params:    paramKey=param space
Path params:    <none>
Multiparts:     <none>

It seems the normal way is to just call .formParam() multiple times to create a list:

    String paramKey = "paramKey";
    String paramWithSpace = "param space";
    Response response = RestAssured.given()
            .formParam(paramKey, paramWithSpace)
            .formParam(paramKey, paramWithSpace)
            .and().contentType("application/json")
            .log().method().log().path().log().parameters()
            .when().post(MessageFormat.format(registrationUrl, testName));

Output:

Request method: POST
Request path:   http://localhost:8080/registrionURL
Request params: <none>
Query params:   <none>
Form params:    paramKey=[param space, param space]
Path params:    <none>
Multiparts:     <none>

Does anyone know how to send a list of length one as a form parameter?

如果您将表单参数作为List传递,它可以工作:

.formParam(paramKey, Arrays.asList(paramWithSpace))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM