首页 > 解决方案 > 当 url 是字符串变量时,如何在一个 GET 请求中传递 pathParam、queryParam 和 headers

问题描述

第一个问题:我有一个 API GET 请求,其中包含路径参数、查询参数和标头,我想将我的请求 url 作为字符串变量,我该如何实现?

第二个问题:如何将 pathParams 传递给字符串变量?我研究过如何传递路径参数,但是所有的例子都是get(" http://some_url/ {path}"),我想把url作为一个String。

像 String url = " http://my/request/url ",

如何使用 url+{id}?没有得到http字符串?

    given()
          .contentType(ContentTypeJSON).
    with()
      .pathParams("id", "1").
    when()
      .get("http://my/request/url/{id}").
    then()
      .assertThat().
          .statusLine("HTTP/1.1 200 OK");

标签: getrest-assuredquery-parametersrequest-headerspath-parameter

解决方案


避免返工的简单方法是使用以下代码。

/*
 * We can parameterize it using baseURI and basePath and send a request to get a customer using ID      
 */
        RestAssured.baseURI = "http://parabank.parasoft.com/";
        RestAssured.basePath = "parabank/services/bank/customers";

        //also we can use a path parameter for the same request

        given().contentType(ContentType.JSON).pathParam("customers", "12212").when().get("{customers}/").then().statusCode(200).log().all();

推荐阅读