Is it possible to execute an external program from Postman?

Hi all,

is it possible to run an external command from PostmanĀ“s pre-requesit Script or Test?
Like call a batch file or an .exe? IĀ“m on Windows 10, if that mattersā€¦

Many thanks,
Christian

1 Like

Hey @chrissbaumann,
You can run a local server and write a small snippet like this to execute a batch file

const http = require('http'),
  { exec } = require('child_process');

http.createServer((req, res) => {
  // Give the path of your bat or exe file
  exec('test.bat', (err, stdout, stderr) => {
    console.log({ err, stdout, stderr });
    
    if (err) {
      return res.writeHead(500).end(JSON.stringify(err));
    }
    
    // Output of the script in stdout
    return res.writeHead(200).end(stdout);
  });
}).listen(3000);

And in your pre-request / test script, you can call your local serverā€™s URL (http://localhost:3000 here) that will execute the batch file and return the response for you. Something like this

pm.sendRequest('http://localhost:3000', (err, response) => {
    // This will have the output of your batch file
    console.log(response.text());
})

You can execute exe files in the same way

2 Likes

Thx.
What kind of server is that? How to start it?

Hey @chrissbaumann, Postman can only trigger HTTP requests. There is no direct way to run a local program through Postman. @bhargavkaranam96ā€™s suggestion was to write a Node.js server which runs a local command (your required .exe or shell script) in a child process when you send a request to that server.

In this case, you will have to write that server and keep it running while you send the request from Postman.

That said, can you elaborate a bit more on your workflow, and what these batch files are supposed to do?

Thx.

Ok, understood.
So basically it could be any kind of server, which then starts a process, when being called via Postman, no?

Basically I need to execute the same collections/ workflows in different environemnts (per client). The Postman/ API part is the same for every environment, but thereĀ“s a step in between, where a browser/ webpage needs to opened and some interaction is needed. Automating this interaction via Selenium/ Watir is far less effort, then doing it in Postman, so IĀ“m searching for a possibility to call a browser GUI automation from Postman.

Yes. Thatā€™s the gist of it. You will have to trigger your GUI testing agent through a server. You can also have your server respond with a response code depending on the GUI test success/failure, and then assert on the response in Postman.

1 Like

Awesome, thanks a lot!

Two small issues in your code I think.

In line 2 it has to be const { exec } = require('child_process');

When the started process ends, I get the following error:
TypeError: Cannot read property 'end' of undefined at exec (xxxxxxx\app.js:18:30) at ChildProcess.exithandler (child_process.js:285:7) at ChildProcess.emit (events.js:189:13) at maybeClose (internal/child_process.js:970:16) at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
which is refering to this line return res.writeHead(200).end(stdout);

When I comment the line, itĀ“s working w/o failure, but I assume, this is delivering the result/ output of the batch, so w/o that, I cannot access the result in Postman?

Hi @chrissbaumann, I am stuck at the similar situation (API flow requiring GUI involvement) for one of my client. Can you please confirm the solution stated above was working? Also, which server did you use to run it? And what was used for GUI testing

Hi @ptarte23 ,

got as far as described in my last entry: Got the server running in node.js, but didnĀ“t manage to get around the TypeError.
So I went automating the web page via Postman, which is working fine and running daily in our builds for more than a a year now.

This article helped me in a workaround solution for an issue in Newman related to the URLEncoded body. Thank you very much Bhargav, Christianbaumann and Kaustavdm.

1 Like

It appears that child_process is no longer allowed in pre-request scripts. Anyone have a work around?