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