当前位置: 首页 > 知识库问答 >
问题:

如何使用Rest Assured以JSON格式获取get请求的响应

陈昊昊
2023-03-14

我需要在get请求中以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在get请求中获取JSON格式?

共有1个答案

焦同
2023-03-14

下面的片段可能会对您有所帮助。

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("\\", "");
    }
 类似资料:
  • 问题内容: 我正在尝试将响应数组和响应数组转换为JSON格式。我已经试过被贴在SO和其他网站一样,所有的答案WEB1,web2的增加,然后 但我一直都想与文本格式的输出。有人可以帮我解决这个问题吗? 助手类: 我的控制器: 我尝试打印,它看起来像这样: Json输出:(html / text) 问题答案: 组 在控制器的动作之前。

  • 问题内容: data = { ‘ids’: [12, 3, 4, 5, 6 , …] } urllib2.urlopen("http://abc.com/api/posts/create”,urllib.urlencode(data)) 我想发送POST请求,但是其中一个字段应该是数字列表。我怎样才能做到这一点 ?(JSON?) 问题答案: 如果您的服务器期望POST请求为json,则您需要添加标

  • 问题内容: 我有一个包含表单(帖子)的html。单击某些提交按钮时,我会收到JSON响应。 如何获取此JSON? 如果我不截取请求,则json将显示在Web视图上,所以我想应该使用(我正在使用API​​ 12),但是我不知道如何在其中获取json。 还是有更好的方法,例如拦截响应而不是请求? 谢谢 问题答案: 您应该重写的方法

  • 我在用放心做API自动化。我想从API响应中获取值并传递给另一个java类, 这是我的测试类, 如何将此方法更改为另一种类型的无效值,或者如何在另一个java类中传递人员引用值

  • 我正在开发一个与我编写的RESTful网络服务通信的Android应用程序。在GET方法中使用Volley非常简单,但是我不能把我的手指放在POST方法上。 如何从我的post请求响应中获取字段这是我的响应 我想只有"访问令牌"内容 这是我的密码 请帮帮我?!

  • 我正在尝试为Rest-Assured(RESTful API)生成cucumber报告,并且我还热衷于捕获cucumber报告中的请求/响应。我想知道是否有人已经实现了这一点或可以提供一些指示。