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

时间:2017-01-19 22:45:01

标签: java rest-assured

我正在尝试通过POST向使用RestAssured的端点提交一些JSON。 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>

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

    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 个答案:

答案 0 :(得分:0)

如果您将表单参数作为List传递

,则它可以正常工作
.formParam(paramKey, Arrays.asList(paramWithSpace))