Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 804
GreyndBlue Avatar asked Aug 19 '18 17:08

GreyndBlue


People also ask

How do I respond to a Postman in HTML?

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.

How do you get the response body in the Postman?

The Postman Body tab gives you several tools to help you understand the response quickly. You can view the body in one of four views: Pretty, Raw, Preview, and Visualize. in the results pane. You can also place your cursor in the response and select ⌘+F or Ctrl+F.


1 Answers

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.

like image 97
Danny Dainton Avatar answered Sep 23 '22 20:09

Danny Dainton