Skip to content Skip to sidebar Skip to footer

ReferenceError: Fetch Is Not Defined - Postman

In Postman, I run an arbitrary request. I put the following code in either the Pre-req. script or in the Tests script: fetch('https://jsonplaceholder.typicode.com/todos/3') .then

Solution 1:

Does Postman not implement the Fetch API?

I think not. The closest correspondence to the fetch() command is pm.sendRequest().
But pm.sendRequest returns a pm object and not a Promise, at least for now.

I have found a workaround, though. In the code snippet below I define the pmFetch() function which is meant to do what the fetch() command does in a normal web browser.

pmFetch('https://jsonplaceholder.typicode.com/todos/3')
  .then(response => response.json())
  .then(responseBody => {
    console.log('The response body:');
    console.log(responseBody);
    console.log('responseBody.title: "' + responseBody.title + '"');
  });
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function pmFetch (url) {
  return new Promise ((resolve, reject) => {
    pm.sendRequest(url, function (err, response) {
      if (err) reject(err);
      resolve(response);
    });
  });
}

pmFetch mimics the Fetch API

Here is a link to the Postman Collection in case you want to download, import, and run the collection.

Reference:


Post a Comment for "ReferenceError: Fetch Is Not Defined - Postman"