I'm getting the response in html. All tutorials are using:
var y = document.getElementsByClassName('mw-search-result-heading');
or
var jsonData = JSON.parse(response);
and they both fail.
First one due to document not defined, second because response is not JSON but HTML.
So, how can I correctly parse an html in Postman ?
Hi you can use cheerio jQuery api
Example:
const $ = cheerio.load(pm.response.text();
console.log($("title").text()); // get title
Related
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:""};
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
I am trying to get the sess_id from the html response in postman.
this is my test.
var test3 = cheerio(pm.response.text());
var variabile = test3.find('[pageId="/Security/SelectPaymentMethod?sess_id=SbyYFpsCefH3hVD3KNB7JwJ7VsYxdNBA1PKDbDJugmNZZyNZIJZeVdAb9H1neJfarNelwg6qXZx&paymentMethodCode=VEC&paymentMethodTypeCode=QTA&environment=Test"]').val();
console.log(variabile('sess_id').val());
HTML
<link accesskey="1" pageId="/Security/SelectPaymentMethod?sess_id=HnapitQl8k9CBhiCkeaKPQP681rb8kFkHmgZKYRbxNw9SxMMcUtwpsKn5bA2s2drwwBzv3S3W1T&paymentMethodCode=VEC&paymentMethodTypeCode=QTA&environment=Test">Verve eCash - Active</link>
in my console I get the response
TypeError | variable is not a function
This is a different and horrible way you could get that value:
let resData = xml2Json(pm.response.text());
let filteredData = resData.link.$.pageId.split('=')
console.log(filteredData[1].split('&')[0])
It's using xml2Json and passing that the response as text, it's then traversing down the data and splitting the pageId value. Not clean at all and I wouldn't use this anywhere other than figuring out a better solution but it's proving it can be done.
Thanks Danny for the effort.
Though I figured out another way.
I converted the xml to JSON first of all and made the test in JSON
let resp = xml2Json (pm.response.text()),
ussdsessionId = resp.page.navigation.link['2'].$.pageId;
console.log({ ussdsessionId });
pm.environment.set("sessionBank", ussdsessionId);
a need parse son from alfresco API, but I have problem with two slashes in URL model address. How can I get value from "prejimka.stavHodnoceni"? Can anyone help me ?
My javascript:
var nazevSlozky = null;
var result = remote.call("/api/metadata?nodeRef=workspace://SpacesStore/7d2eab73-9500-406a-bdb0-40209924b2d2");
var json = JSON.parse(result);
var nazevSlozky = json.properties.{http://ourFirm.cz/model/someFirm/3.0.}prejimka.stavHodnoceni;
JSON:
My json
Try json.properties['sf:prejimka.stavHodnoceni'] where "sf" is the model abbreviation.
Also, I don't think you want to use punctuation in your property names. I'm not sure it is expressly forbidden but it seems like a bad idea.
I have an html output in the form of a table and I want to send that with my JSON eventually via a POST for an api.
I have removed tags and troubleshot it but doesnt work. Is there a specific library that I can use in R to achieve this parsing?
I am using simple paste() to parse that html page and insert it into JSON's 'value'.
I first save my data.frame(list) as html then I try to parse it.
mydoc = bsdoc( title = 'xxx' )
mydoc= addFlexTable( mydoc, flextable = output_as_flex)
mydoc = addParagraph( mydoc, value = "<br>")
writeDoc(mydoc,file ="C://yyy.html")
tree=htmlTreeParse("C://yyy.html")
Here I follow the Confluence Rest Api's format to form this string for JSON. Data has to be passed(I am passing a variable in paste) as html where 'h_h'
data <- paste(sep='','{"type":"page","title":',
'"xxx",',
'"ancestors":[{"id":',123123,'}],"space":{"key":"','xxx','"},"body":{"storage":{"value":"',h_h,'","representation":"storage"}}}')
httr::set_config(config(ssl_verifypeer=FALSE))
URL <- "xxx/rest/api/content"
response = POST(URL, authenticate("xx", "xx"), body = data, verbose(),add_headers('Content-Type' = 'application/json;charset=utf-8','Cache-Control'='no-cache'))
response
http_status(response)
warn_for_status(response)
stop_for_status(response)
I want to upload that html table formed as output_as_flex to the api and eventually to the confluence page.
Thanks.
I would like to know how to parse JSON in phantomjs. Any page content is enclosed in html (<html><body><pre>{JSON string}</pre></body></html>). Is there an options to remove enclosing tags or asking for a different Content-Type as "application/json"? If not, what's the best way to parse it. Is it using jQuery after including with includeJS jQuery?
Since you are using PhantomJS which is built of the webkit browser you have access to the native JSON library. There is no need to use page.evaluate, you can just use the plainText property on the page object.
http://phantomjs.org/api/webpage/property/plain-text.html
var page = require('webpage').create();
page.open('http://somejsonpage.com', function () {
var jsonSource = page.plainText;
var resultObject = JSON.parse(jsonSource);
phantom.exit();
});
Here is what I did:
var obj = page.evaluate(function() {
return eval('(' + document.body.innerText + ')');
}
Then the obj you got is the JSON object returned from that page.
As already in the accepted answer, I would suggest using JSON.parse() for converting a JSON string into an object.
For example, your code could look like this:
var jsonObject = page.evaluate(function() {
return JSON.parse(page.plainText);
});
If the json data contains html strings, they will be removed within the suggested page.plainText attribute.