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

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

1 Like

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. @bhargav.karanam’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?