Postman: How to extract value from html response and pass it on to next request in postman
Asked Answered
S

3

11

Example url: https://abc.xyz.com/m# HTML Response:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
.
.
</head>
<body class="abc">        
.
.
.
<script>xab.start('{\"first\":\"123xyz\",\"second\":\"abc123\",\"third"..;</script>
</div>
</body>
</html>

In the above mentioned response i want to extract the value of the parameter second '("second\":\"abc123\")' from the response and pass it on to the next request.

It would be simpler if the response is JSON, but in the case this is HTML response.

I was able to do this on JMeter using Regex but having hard time to do it on Postman.

Thanks!

Scholl answered 19/8, 2018 at 17:6 Comment(3)
You could look at using Cheerio to get the values, it's one of the built modules in Postman - github.com/cheeriojs/cheerio/blob/master/Readme.mdAlexandrite
I am unable to get the value with Cheerio, could you please let me know how to do it.Scholl
Updated with an answer and a usage example.Alexandrite
A
20

You could look at using Cheerio to get the values, it's one of the built in modules within the Postman native application.

You could add something like this example, to extract the value from the HTML.

This is getting the value from the title html tag, of the jsonplaceholder page, then setting it as an environment variable:

const $ = cheerio.load(pm.response.text())

pm.test("it should return a title", () => { 
    pm.expect($('title').text()).to.not.be.empty 
})

pm.environment.set('title', $('title').text())

Postman_Cheerio

I'm sure you could use this to get the value you need from your example.

Alexandrite answered 20/8, 2018 at 13:17 Comment(2)
It worked with html, but not with above posted example.Scholl
My answer was an example of what you could do to get the value from a HTML page. Not a perfect solution to solve your specific problem.Alexandrite
D
-1

my example how I get data from script tag in html response

const $ = cheerio.load(pm.response.text())
var script = ($('script').text().replace("window.__STATE__ = ",""));

var jsonData = JSON.parse(script);

var uid = pm.environment.get("uid");
if (jsonData.stared == uid) pm.environment.set('reactArticle', false);
else {pm.environment.set('reactArticle', true);}
Deicide answered 20/2, 2019 at 1:23 Comment(0)
A
-2

Try to wrap the response through Json.parse and then splice this response using the splice method of JavaScript of substring whichever you feel comfortable.

After that save the same in a variable by setVariable method and then you can use it in another requests.

Auckland answered 21/8, 2018 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.