如何使用 Rest Assured 在 GET url 中传递查询字符串参数?
Posted
技术标签:
【中文标题】如何使用 Rest Assured 在 GET url 中传递查询字符串参数?【英文标题】:How to pass Query String Parameters in GET url using Rest Assured? 【发布时间】:2017-04-26 09:42:49 【问题描述】:网址为:http://example.com/building
我的查询字符串是:
globalDates:"startMs":1473672973818,"endMs":1481448973817,"period":90
limitTo:6
loadTvData:true
startFrom:0
userId:5834fb36981baacb6a876427
【问题讨论】:
【参考方案1】:QueryParam can be passed as:
String endpoint = "http://example.com/building";
var response = given()
.queryParam("globalDates", "\"startMs\":1473672973818,\"endMs\":1481448973817,\"period\":90")
.queryParam("startFrom", "0").queryParam("limitTo", "6").queryParam("loadTvData", true)
.queryParam("startFrom", "0").queryParam("userId", "5834fb36981baacb6a876427")
.when().get(endpoint).then();
【讨论】:
【参考方案2】:对于我们的 api,类型如下:
https://my.api.com/meeting?page=0&size=1
我们有:
Response response = requestSpecification.queryParam("page", 0).queryParam("size", 1).get(baseEndpoint);
【讨论】:
【参考方案3】:您也可以将这个查询参数放在 Map 中,如下所示,
HashMap<String, String> params = new HashMap<String, String>()
put("globalDates", "\"startMs\":1473672973818,\"endMs\":1481448973817,\"period\":90");
put("limitTo","6" );
,...
然后像下面这样发布,
resp = RestAssured.given()
.headers(headers)
.queryParameters(params)
.post(apiURL).andReturn();
【讨论】:
谢谢你,正是我想要的! :)【参考方案4】:您可以像这样使用 Rest Assured 在 GET URL 中传递查询字符串参数:
when()
.parameter("globalDates","startMs","1474260058054","endMs","1482036058051","period","90")
.parameters("limitTo","6")
.parameters("loadTvData","true")
.parameters("startFrom","0")
.parameters("userId","5834fb36981baacb6a876427");
【讨论】:
parameter()
和 parameters()
方法目前已被弃用。【参考方案5】:
您可以将它们作为 queryParam 传递..
given()
.queryParam("globalDates", "\"startMs\":1473672973818,\"endMs\":1481448973817,\"period\":90")
.queryParam("startFrom", "0").queryParam("limitTo", "6").queryParam("loadTvData", true)
.queryParam("startFrom", "0").queryParam("userId", "5834fb36981baacb6a876427")
.when().get("http://example.com/building"). ...
【讨论】:
以上是关于如何使用 Rest Assured 在 GET url 中传递查询字符串参数?的主要内容,如果未能解决你的问题,请参考以下文章
rest-assured之获取响应数据(Getting Response Data)
rest-assured的默认值与Specification重用