3

I'm trying to recreate a scenario with the postman and there is a _csrf value in the previous GET request response body to be passed with the next POST request.

I Can't find a way to extract the value from POSTMAN.

NOTE: What I want is something similar to Regular Expression Extractor in Jmeter.If you have any Idea about extracting a value form the response body and setting it to a variable. Please let me know.

Cheers, Muditha

Muditha Perera
  • 1,216
  • 8
  • 24

3 Answers3

7

This might help you https://media.readthedocs.org/pdf/postman-quick-reference-guide/latest/postman-quick-reference-guide.pdf

They use Cheerio

2.2.5 How to parse a HTML response to extract a specific value? Presumed you want to get the _csrf hidden field value for assertions or later use from the response below:

To parse and retrive the value, we will use the cherrio JavaScript library:

responseHTML = cheerio(pm.response.text()); console.log(responseHTML.find('[name="_csrf"]').val());

Cheerio is designed for non-browser use and implements a subset of the jQuery functionality. Read more about it at https://github.com/cheeriojs/cheerio

Sugan
  • 447
  • 1
  • 4
  • 16
0
responseHTML = cheerio(pm.response.text());
var po= responseHTML.find('[name="_csrf"]').val();
console.log(po);
pm.environment.set("token", po);

/* You need to set the environment in Postman and capture the CSRF token in variable "here po" using a get request. Next in post request the environment variable token can be used */

0

Just made this JS in post man to parse Without a REGEx. Hope it will help people in the futur

Text to parse : Json : Extract data-id :

{
    "code": "OK",
    "response": {
        "append": {
            "html": {
                "< .folders": "<a class=\"folder\" href=\"/foobarfoo\" data-id=\"ToExtract\"><div><i class=\"far fa-fw fa-folder\"></i></div><div class=\"folder-name\">blabla</div><div><div class=\"badge\">0</div></div></a>"
            }
        }
    }
}
console.log(responseBody.response);

var jsonData = JSON.parse(responseBody);
var iStart = responseBody.indexOf("response\":")+10;
var scenarioId = responseBody.substr(iStart,10);
var iEnd = scenarioId.indexOf("}");
var scenarioId = scenarioId.substr(0,iEnd);

console.log("scenarioId:" + scenarioId + "iStart: "+ iStart + " scenarioId : " + scenarioId);
pm.environment.set("scenario", scenarioId);