使用 RestAssured 将一个项目列表作为 formParam 发送

Sending one item list as formParam with RestAssured

我正在尝试通过 POST 将一些 JSON 提交到具有 RestAssured 的端点。 API 需要一个键列表。我需要为此发送的值是一个包含单个项目的列表。

理想的请求应该是这样的:

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>

如果我将参数添加为列表或映射,它仍然只是将参数作为字符串发送:

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

输出:

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>

似乎正常的方法是多次调用 .formParam() 来创建一个列表:

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

输出:

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>

有谁知道如何将长度为 1 的列表作为表单参数发送?

如果您将表单参数作为 List:

传递,它会起作用
.formParam(paramKey, Arrays.asList(paramWithSpace))