Script Calling Other Languages (e.g., Java, Python, PHP)
Pre-request and Post-response Scripts
Pre-request and Post-response scripts can directly invoke external programs written in the following languages:
java (.jar)
python (.py) // Note: Python 2 is currently the only version supported
php (.php)
js (.js)
BeanShell (.bsh)
go (.go)
shell (.sh)
ruby (.rb)
lua (.lua)
Usage Method
The method pm.execute(fileName, args)
is used within scripts to call external programs.
Parameter
fileName: String
, the absolute path of the external program file name.Parameter
args: Array<String>
, the arguments passed to the external program, which are of string array type, allowing multiple arguments to be passed.
Return value: String
, the string output in the console when the program is run from the command line.
Ensure that the corresponding environment for running the respective program is installed on the computer.
.jar programs: Require the Java environment to be installed.
.py programs: Require the Python environment to be installed.
.js programs: Require the Node.js environment to be installed.
Other language programs: Require the environment for the corresponding language to be installed.
Invocation Principle
Invoking an external program runs it through the command line, and the return value is the string output from the program in the console. The system automatically invokes the appropriate command line based on the file extension of the external program.
.jar programs: Run through the java command.
For example: The script `pm.execute('cn.example.Demo.jar', ['abc','bcd'])` actually executes the command `java -jar cn.example.Demo.jar abc bcd`.
If you need to call a specific method in the jar, execute: `pm.execute('cn.example.Demo.jar', ['abc','bcd'], {
className: 'com.example.Demo',
method: "sayHi"
})`, which actually executes the `sayHi` method of the `com.example.Demo` class in the `cn.example.Demo.jar` file, with the parameters `['abc','bcd']`.
.py programs: Run through the python command.
For example: The script `pm.execute('md5-json.py', ['abc','bcd'])` actually executes the command `python md5-json.py abc bcd`.
.js programs: Run through the node command.
For example: The script `pm.execute('xxx.js', ['abc','bcd'])` actually executes the command `node xxx.js abc bcd`.
Other language programs follow a similar principle.
Code Example
Post-response scripts:
try {
// PHP example, calling demo.php
// Note: When JSON-formatted data is used as an argument, it is necessary to serialize the argument using JSON.stringify
// The actual command line executed is: php demo.php '{"a":1,"b":2}'
const phpResultString = pm.execute("demo.php", [JSON.stringify({ a: 1, b: 2 })]);
// Note: When the returned data is a JSON-formatted string, it can be deserialized using JSON.parse
const phpResult = JSON.parse(phpResultString);
console.log("php execution result", phpResult);
} catch (e) {
console.error(e.message);
}
test.php code:
<?php
$param = json_decode($argv[1]);
$result = [];
foreach($param as $key => $value) {
$result[$key] = $value * 2;
}
echo json_encode($result);