Hi guys, I’m getting a problem with HTML response. In details, I want to get the value in below case .
How can I do this? I knew exactly the regular expression to get it but do not know how to apply? Thanks all
Welcome @nguyenvietdungqsq!
If you already have the regex and just need the response as a string in order to apply it, you can get it in the tests tab of Postman by using the following command:
const responseText = pm.response.text();
const matches = responseText.match(<your regex>);
const sessionId = matches[0];
This assumes it will always be there and always be the first one.
I do have a concern though - what you’re doing seems unsafe and hacky. From what I can piece together, you are doing a GET on an html page and essentially scraping it for a hardcoded value?
If you’re designing that webpage, you should not store that value like that. Because if you can do this through Postman, so can malicious users.
Welcome to the community!
In addition you can use cheero.js to load the HTML, and parse your results.
const response = cheerio.load(pm.response.text(), {
ignoreWhitespace: true,
xmlMode: true
});
const sessionId = response('script').find('sessionId')
The above may not working exactly, as I am shooting from the hip, but you can use cheerio to parse through your html and get to your element and then parse out the js from there.
However, I will have to agree with @allenheltondev, and having a sessionID in the js script of a html page is not good practice, and a bit unsafe. That information is better sent in headers or a JWT. I am no professional with communicating session IDs with web pages, however, from my experience, seeing it in the script portion of an html page is uncommon for me.
Hope this helps!
Thank bro, it is great. Actually my project has been built for a long time so that it still includes a lot of issues with security. Of course, I will ask my project manager for handling a new safety. Again,thank you a lot
Thank bro, it is nice. I already know the problem when letting SessionId being in HTML body. I will ask my PM for requesting for a change to be safer. Thank you.
Any time, glad I could help
lets say url is /appstest/RunUpdate?id=26216&requestId=1614895894455&ln=1000043845
want to print id, requestId, ln
values only in console
How to extract all the href links with submit linknames from below html response
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>appstest Import System: Loans Processed</title>
<link rel="stylesheet" type="text/css" href="/appstest/css-table.css">
<script type="text/javascript" src="/appstest/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="/appstest/appstest.js"></script>
</head>
<body>
<table width="100%"><caption>Loans in process queue (Sorted by last modified)</caption>
<thead><tr>
<th scope="col">File Name</th>
<th scope="col">Loan Number</th>
<th scope="col">Date Created</th>
<th scope="col">Last Modified</th>
<th scope="col">Request</th>
</tr></thead><tbody>
<tr><td>FILECheckTestLoans.xlsx</td><td><a href=/LoanServicing/GetLoanData?loannumber=1000043845 target=loanDetail>1000043845</a></td><td>2021-03-04 15:37:49.437</td><td>2021-03-04 15:37:49.437</td><td><a href=/appstest/RunUpdate?id=26088&requestId=1614893866191&ln=1000043845 target=_new>Submit</td></tr><tr><td>FILECheckTestLoans.xlsx</td><td><a href=/LoanServicing/GetLoanData?loannumber=1000043833 target=loanDetail>1000043833</a></td><td>2021-03-04 15:37:49.437</td><td>2021-03-04 15:37:49.437</td><td><a href=/appstest/RunUpdate?id=26089&requestId=1614893866191&ln=1000043833 target=_new>Submit</td></tr><tr><td>FILECheckTestLoans.xlsx</td><td><a href=/LoanServicing/GetLoanData?loannumber=1000043832 target=loanDetail>1000043832</a></td><td>2021-03-04 15:37:49.437</td><td>2021-03-04 15:37:49.437</td><td><a href=/appstest/RunUpdate?id=26090&requestId=1614893866191&ln=1000043832 target=_new>Submit</td></tr><tr><td>FILECheckTestLoans.xlsx</td><td><a href=/LoanServicing/GetLoanData?loannumber=1000043831 target=loanDetail>1000043831</a></td><td>2021-03-04 15:37:49.437</td><td>2021-03-04 15:37:49.437</td><td><a href=/appstest/RunUpdate?id=26091&requestId=1614893866191&ln=1000043831 target=_new>Submit</td></tr><tr><td>FILECheckTestLoans.xlsx</td><td><a href=/LoanServicing/GetLoanData?loannumber=1000043845 target=loanDetail>1000043845</a></td><td>2021-03-04 15:37:49.437</td><td>2021-03-04 15:37:49.437</td><td><a href=/appstest/RunUpdate?id=26092&requestId=1614893866191&ln=1000043845 target=_new>Submit</td></tr><tr><td>FILECheckTestLoans.xlsx</td><td><a href=/LoanServicing/GetLoanData?loannumber=1000043833 target=loanDetail>1000043833</a></td><td>2021-03-04 15:37:49.437</td><td>2021-03-04 15:37:49.437</td><td><a href=/appstest/RunUpdate?id=26093&requestId=1614893866191&ln=1000043833 target=_new>Submit</td></tr><tr><td>FILECheckTestLoans.xlsx</td><td><a href=/LoanServicing/GetLoanData?loannumber=1000043832 target=loanDetail>1000043832</a></td><td>2021-03-04 15:37:49.437</td><td>2021-03-04 15:37:49.437</td><td><a href=/appstest/RunUpdate?id=26094&requestId=1614893866191&ln=1000043832 target=_new>Submit</td></tr><tr><td>FILECheckTestLoans.xlsx</td><td><a href=/LoanServicing/GetLoanData?loannumber=1000043831
Hey @gpub1
Would this give you what you need?
var cheerio = require('cheerio');
$ = cheerio.load(pm.response.text());
links = $('a');
$(links).each(function (i, link) {
console.log(i + ': ' + $(link).attr('href'));
});
I mocked out the request using your response example:
Used this SO answer as a reference:
hey @danny-dainton good to see you yeah 50% … its giving all the links.
how about only with submit links ?
How about trying the figure that out from the example that you have
How do you think you would achieve it?
I can tell you an answer but I would much prefer you to have a go yourself and learn that way.
I guarantee it will be more beneficial in the long run
I have no context about any of those links but if I didn’t want to log the ones that contained LoanServicing, I might do it like this…
$ = cheerio.load(pm.response.text());
links = $('a');
$(links).each(function (i, link) {
if(!$(link).attr('href').includes('LoanServicing')){
console.log(i + ': ' + $(link).attr('href'));
}
});
Damn !! you killed it as always!! will work around without ‘LoanServicing’ Thanks bro
Thanks @danny-dainton for stepping in.
None other than the man himself with his javascript acumen and beat me to it!
I would agree with him @gpub1, giving it a whirl yourself certainly helps in the long one.
Once you can the anchor tags (‘a’) and hrefs, you can do whatever manipulation thats available via native javascript functions and cheerio.js functions.
Their documentation is pretty comprehensive. https://cheerio.js.org
Pl ignore my ask @danny-dainton.