ReferenceError: XMLHttpRequest is not defined

I generated a piece of java script from postman.

The 1st line of the script is:

var xhr = new XMLHttpRequest();

However, I pasted the script into the prescript of another API call. The script generates the following error:

There was an error in evaluating the Pre-request Script: ReferenceError: XMLHttpRequest is not defined

I must be misunderstanding something.
Has anyone else encountered this before?

Postman runs the “Pre-request” and “Test” scripts within a Sandbox.

XMLHttpRequest is part of client-side/browser-oriented Javascript Web API. Postman Sandbox APIs draw from Node.js, and only a fixed set of APIs are available within this sandbox.
Refer to the following documentation for reference: https://learning.getpostman.com/docs/postman/scripts/postman-sandbox-api-reference/

If you are looking to make an async request, try using pm.sendRequest() instead.

Else, do provide with more context around your use-case and I’ll have a look.

It’s true what @amit mentioned, but you can use pm.sendRequest to make API calls from the scripts.
Here are a few examples: https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990

1 Like

First of all thanks for the link to the function pm.sendRequest()

My goal is to download directly into a local folder, a very big data file beyond the GET method

Like it is possible from this UI function but I need into Tests script tab

If you see this following code

I am considering your example with this big file as url
const downloadUrl = "https://www3.stats.govt.nz/2018census/Age-sex-by-ethnic-group-grouped-total-responses-census-usually-resident-population-counts-2006-2013-2018-Censuses-RC-TA-SA2-DHB.zip";

// Example with a plain string URL
pm.sendRequest(downloadUrl, (error, response) => {
  if (error) {
    console.log(error);
  } else {
  console.log(response);
  }
});

How the function should be look like, to download the big file directly into a local folder?