如何从 REST ASSURED 框架中的 GET 调用中提取 html 响应并解析它 JSON?
How to extract a html response and parse it JSON from a GET call in REST ASSURED framework?
在 Postman 中,我已经按照以下方式做了,并且想在 Rest Assured 框架中做同样的事情。我想在 Rest Assured 框架中解析并保存“tltkt”值。我怎样才能在 Rest Assured 中做到这一点?
GET 调用:https://prod.streaming/com/account/signin/
邮递员测试:**
将 HTML 响应加载到 $
const $ = cheerio.load(pm.response.text())
console.log($("title").text()); // get title
console.log($('script#app-config').text().trim());
let appConfig = JSON.parse($('script#app-config').text().trim());
console.log(appConfig.tltkt);
pm.collectionVariables.set("saved_tl_tkt", appConfig.tl_tkt);
console.log(pm.collectionVariables.get("saved_tl_tkt"), ":from pm");
HTML中的响应:
main id="main-container"
script id="app-config" type="application/json"
{"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""};
我假设你已经 {"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""}
因为它无效json,所以我使用正则表达式从这个字符串中提取值。
String bodyTxt = ...;
Pattern pattern = Pattern.compile("\"tltkt\":\"(.+?)\"");
Matcher matches = pattern.matcher(bodyTxt);
String tltkt = "";
if(matches.find()) {
tltkt = matches.group(1);
}
System.out.println(tltkt);
//QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ
在 Postman 中,我已经按照以下方式做了,并且想在 Rest Assured 框架中做同样的事情。我想在 Rest Assured 框架中解析并保存“tltkt”值。我怎样才能在 Rest Assured 中做到这一点?
GET 调用:https://prod.streaming/com/account/signin/
邮递员测试:** 将 HTML 响应加载到 $
const $ = cheerio.load(pm.response.text())
console.log($("title").text()); // get title
console.log($('script#app-config').text().trim());
let appConfig = JSON.parse($('script#app-config').text().trim());
console.log(appConfig.tltkt);
pm.collectionVariables.set("saved_tl_tkt", appConfig.tl_tkt);
console.log(pm.collectionVariables.get("saved_tl_tkt"), ":from pm");
HTML中的响应:
main id="main-container"
script id="app-config" type="application/json"
{"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""};
我假设你已经 {"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""}
因为它无效json,所以我使用正则表达式从这个字符串中提取值。
String bodyTxt = ...;
Pattern pattern = Pattern.compile("\"tltkt\":\"(.+?)\"");
Matcher matches = pattern.matcher(bodyTxt);
String tltkt = "";
if(matches.find()) {
tltkt = matches.group(1);
}
System.out.println(tltkt);
//QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ