Home > Net >  How to extract a html response and parse it JSON from a GET call in REST ASSURED framework?
How to extract a html response and parse it JSON from a GET call in REST ASSURED framework?

Time:03-08

In Postman I have did in the below way and want to do the same in Rest Assured framework. I want to parse and save the "tltkt" value in Rest Assured framework. How can I do that in Rest Assured?

GET call :https://prod.streaming/com/account/signin/

Postman tests:** Load the HTML response to $

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");

Response in HTML:

main id="main-container"
script id="app-config" type="application/json"

{"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""};

CodePudding user response:

I assume that you've already had {"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""}

Because it's not valid json, so I use regex to extract value from this string.

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
  • Related