Parse HTML form response data

I need to parse the html response data but the html to json conversion methods are not working. Here is sample response data:
Looking to grab the csrftoken value from the html.

   <form action="" method="post">
        <input type='hidden' name='csrftoken' value='KX3x6gY56wVcK4O2' />
        <tr><th><label for="id_username">Username:</label></th><td><input type="text" name="username" maxlength="254" required autofocus id="id_username" /></td></tr>
    <tr><th><label for="id_password">Password:</label></th><td><input type="password" name="password" required id="id_password" /><input type="hidden" name="next" id="id_next" /></td></tr>
        <input type="submit" value="Submit" />
    </form>

@jayanjoseph
here you can use cheerio jQeury api

const $ = cheerio.load(responseBody);

console.log($( “input[name=‘csrftoken’]” ).val());

reference: https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox

Thank you, that worked. Awesome.

Hi,

when I execute const $ = cheerio.load(responseBody); then $ is null.
Any ideas?

Thanks,
Christian

Basically, you need to either pass in a selector, in order to see the HTML data in the console/anywhere else or using one of the functions of the Cheerio API like $.html().

More information about the Cheerio module can be found here - I’ve added an extract from that site below just to show one method of using Cheerio.

Using this HTML markup as an example:

<ul id="fruits">
  <li class="apple">Apple</li>
  <li class="orange">Orange</li>
  <li class="pear">Pear</li>
</ul>

Selectors

Cheerio’s selector implementation is nearly identical to jQuery’s, so the API is very similar.

$( selector, [context], [root] )

selector searches within the context scope which searches within the root scope. selector and context can be a string expression, DOM Element, array of DOM elements, or cheerio object. root is typically the HTML document string.

This selector method is the starting point for traversing and manipulating the document. Like jQuery, it’s the primary method for selecting elements in the document, but unlike jQuery it’s built on top of the CSSSelect library, which implements most of the Sizzle selectors.

$('.apple', '#fruits').text() 
//=> Apple 
$('ul .pear').attr('class') 
//=> pear 
$('li[class=orange]').html() 
//=> Orange

Just as a quick example of extracting a value from a HTML page using Cheerio:

3 Likes

Thanks Danny,

  1. I didn´t ask my question clear enough, I was checking for $.html
  2. I forgot the brackets, so $.html**()** works.
1 Like

Thanks, this has been a great help for me to capture individual form responses. Is there a way to dynamically iterate through a form to capture all name and values to create as variables? Or is it easier capturing the whole form and passing that into the next API call directly?

I need to do something similar but I need to extract a token and set an environment variable from the HTML from something like this:
image

Cheerio syntax here didn’t quite get me there, though I keep poking. Any ideas?

const $ = cheerio.load(responseBody);

console.log($(“script.[name=‘localStorage.setItem’]”).val());
is as close is I’ve gotten

Based on the discussions here, we’ve created this collection that demonstrates how to use the cheerio library to parse an HTML response:

https://www.postman.com/postman/workspace/postman-answers/collection/9215231-cd114167-c5a1-495e-a50f-29d6dd5af4e0?ctx=documentation

You can fork it to your personal workspace and try it out directly. :slightly_smiling_face:

1 Like