【问题标题】:How get response on get request in JSON format using Rest Assured如何使用 Rest Assured 以 JSON 格式获取获取请求的响应
【发布时间】:2018-06-26 09:33:20
【问题描述】:

我需要在获取请求时以 JSON 格式获取数据,数据应为以下格式:

{
  "birth_date": "1980-01-01",
  "birth_place": "",
  "sex": "",
  "inn": "",
  "snils": "",
  "rezident": "0",
  "depend_count": null,
  "name": {
    "surname": "Заноза",
    "name": "Заноза",
    "patronymic": "Заноза"
  }
}

但是数据以如下形式返回,响应为字符串:

{
  "birth_date" : "1980-01-01"
,
  "birth_place" : ""
,
  "sex" : ""
,
  "inn" : ""
,
  "snils" : ""
,
  "rezident" : "0"
,
  "depend_count" : null,
  "name" : {
    "surname" : "\u0417\u0430\u043D\u043E\u0437\u0430"
,
    "name" : "\u0417\u0430\u043D\u043E\u0437\u0430"
,
    "patronymic" : "\u0417\u0430\u043D\u043E\u0437\u0430"

  }
}

代码:

public static String getClientInfo(String clientId) {
   String response =  given().
          param("id", clientId).
          when().get("/services/clients").               
          then().assertThat().statusCode(200).and().
          extract().body().asString();
          System.out.println("Response = " + response);
          return response;
}

如何使用 Rest-Assured 在获取请求时获取 JSON 格式?

【问题讨论】:

    标签: json rest api rest-assured


    【解决方案1】:

    下面的 sn-p 可能会对你有所帮助。

    public static String getClientInfo(String clientId) {
            Response response =  given().
                    param("id", clientId).
                    when().get("/services/clients");
            response.then().assertThat().statusCode(200);
            System.out.println("Non pretty Response : " + response.body().asString());
            System.out.println("Pretty Response : " + response.prettyPrint().toString());
            return response.prettyPrint().toString().replace("\\", "");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      相关资源
      最近更新 更多