【问题标题】:In Rest Assured while trying to get token facing issue "Failed to parse the JSON document"尝试获取令牌时遇到问题“无法解析 JSON 文档”,请放心
【发布时间】:2021-06-25 20:23:07
【问题描述】:
public void appTocken() {
RestAssured.baseURI ="https://localhost";
RequestSpecification request = RestAssured.given();

String payload= "{   \"client_id\": \"ahsan@gmail.com\",\r\n"
        + "    \"client_secret\": \"1212\",\r\n"
        + "    \"isInternalUser\": true,\r\n"
        + "    \"grant_type\": \"client_credentials\"\r\n"
        + "}";
request.header("Content-Type", "application/json");
Response responseFromApp =  request.body(payload).post("/auth/oauth/oidc/login-token");
responseFromApp.prettyPrint();

String jsonString = responseFromApp.getBody().toString(); String tockenGenerated = JsonPath.from(jsonString).get("access_token");

}

【问题讨论】:

  • jsonstring 中有什么
  • @PDHide 提取令牌我将其转换为字符串。另请注意,当我尝试在 responseFromApp.prettyPrint() 上方在线打印时;我什么都没有。
  • 表示你得到了一个空响应
  • 尝试打印jsonstring,看看有没有值
  • 在打印 jsonString 时显示“io.restassured.internal.RestAssuredResponseImpl@5707c1cb”

标签: api selenium rest-assured rest-assured-jsonpath


【解决方案1】:
String jsonString = responseFromApp.getBody().toString(); 

将对象转换为字符串字符串对象,它没有给出json字符串,使用:

String jsonString = responseFromApp.getBody().asString(); 

如果漂亮的打印是空的,那么 asString() 也将是空的

一个工作示例:

Response maxJson = RestAssured
            .given().header("Content-type", "application/json").body("{\r\n"
                    + "         \"name\": \"morpheus\",\r\n" + "            \"job\": \"leader\"\r\n" + "        }")
            .post("https://reqres.in/api/users");
    maxJson.prettyPrint();

    String jsonString = maxJson.getBody().asString();
    System.out.println(jsonString);

【讨论】:

    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多