在尝试获取面临问题的令牌时放心 "Failed to parse the JSON document"
In Rest Assured while trying to get token facing issue "Failed to parse the JSON document"
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();
字符串 jsonString = responseFromApp.getBody().toString();
字符串 tockenGenerated = JsonPath.from(jsonString).get("access_token");
}
String jsonString = responseFromApp.getBody().toString();
将对象转换为字符串对象,它不提供 json 字符串,使用 :
String jsonString = responseFromApp.getBody().asString();
如果 pretty print 为空则 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);
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();
字符串 jsonString = responseFromApp.getBody().toString(); 字符串 tockenGenerated = JsonPath.from(jsonString).get("access_token");
}
String jsonString = responseFromApp.getBody().toString();
将对象转换为字符串对象,它不提供 json 字符串,使用 :
String jsonString = responseFromApp.getBody().asString();
如果 pretty print 为空则 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);