Skip to main content

Calling Other Languages From Scripts (Java, Python, PHP and etc,.)

The preprocessor and postprocessor scripts can directly call external programs written in the following languages:

  • java (.jar)
  • python (.py)
  • php (.php)
  • js (.js)
  • BeanShell (.bsh)
  • go (.go)
  • shell (.sh)
  • ruby (.rb)
  • lua (.lua)
Please be aware that:

External programs run outside the sandbox environment have permission to access and operate other programs, files, and data on the computer. Therefore, you should be aware of the underlying security risks. You are responsible for ensuring the security of the called programs.

Usage

  1. Copy the external programs (.jar, .py, .php, etc.) that you want to call into the external programs directory. Click the ⚙-shaped icon at the bottom left corner of the app and select External Programs to view the external programs directory.

  2. Use the method pm.execute(fileName, args) to call an external program.

    1. Parameters fileName:String: the name of the external program file. It should be stored in the external program directory.
    2. Arguments args:Array<String>: Parameters to be passed to the external program. It is an array of strings so that you can pass multiple parameters.
    3. Parameter options:JarOptions & CommonOptions (supported from version 0.2.0, check below), an optional parameter passed to pm.execute to configure certain features, such as "calling the specified method in jar packages".
    4. Return value: String: the string output in the console when running from the command line.
    5. An exception is thrown when an error occurs. We recommend using try catch to handle exceptions.

    CommonOptions type:

    interface CommonOptions {
    windowsEncoding?: string // The encoding format used by the Windows, using "cp936" by default
    }

    JarOptions type: Used when calling the java program to specify a specific method for a specific class in the jar package.

    interface JarOptions {
    className: string; // Specify the class name in the jar package, such as "com.apidog.Utils"
    method: string; // Specify the function name in the jar package, such as "add"
    paramTypes?: string[]; // Specify the function parameter type in the jar package, such as ["int","int"]
    }

    Type description:

    1. paramTypes is an optional field. If it was empty, it would follow the args parameter by default, integer as "int", floating point as "double", boolean as "boolean", string as "string", array as the type of the first element, e.g. [3] as "int []", [3.14] as "double []", and so on.
    2. If the auto generated type did not match the parameter type of the called function, you need to manually setup paramTypes.
    3. paramTypes Supported data types are: "Number""int""Integer""long""Long""short""Short""float""Float""double""Double""boolean""Boolean""String""Number[]""int[]""Integer[]""long[]""Long[]""short[]""Short[]""float[]""Float[]""double[]""Double[]""boolean[]""Boolean[]""String[]"
  3. Make sure that your computer has the appropriate environment installed for the program to run.

    1. .jarprograms: you need to install the java environment.
    2. .pyprograms: you need to install the python environment.
    3. .jsprograms: you need to install the nodejs environment.
    4. Other language programs: you need to install the environment of the corresponding language.

The Principle Behind Calling Other Languages From Scripts

  1. Calling an external program is to run the program as it is in the command line, and the return value is the string output by the program in the console.
  2. The system will automatically invoke the corresponding command line to run the external program according to its extension.
    1. .jar: run by java command.
      • Example: if the script is pm.execute('com.apidog.Base64EncodeDemo.jar', ['abc','bcd']), the actual execution command will be java -jar com.apidog.Base64EncodeDemo.jar abc bcd.
    2. .py: run by python command.
      • Example: If the script is pm.execute('md5-json.py', ['abc','bcd']), the actual execution command will be python md5-json.py abc bcd.
    3. .js: run by node command.
      • Example: If the script is pm.execute('xxx.js', ['abc','bcd']), the actual execution command will be node xxx.js abc bcd.
    4. Similar principles apply in other languages as well.

Code Example

Postprocessor Script:

try {
// jar example: calling com.apidog.Base64EncodeDemo.jar
// The actual command is java -jar com.apidog.Base64EncodeDemo.jar abc
const jarResult = pm.execute("com.apidog.Base64EncodeDemo.jar", ["abc"]);
console.log("jar result", jarResult);

// php example: calling test.php
const param1 = { a: 1, b: 2 };
// You must serialize the parameter using JSON.stringfy when using JSON as a parameter.
// The actual command is php test.php '{"a":1,"b":2}'.
const phpResultString = pm.execute("test.php", [JSON.stringify(param1)]);
// If the returned data is in json format, you can use JSON.parse to deserialize it.
const phpResult = JSON.parse(phpResultString);
console.log("php 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);

Call the specified function in the jar package

The code in com.apidog.utils.jar package:

package cn.apifox.utils;

public class Utils {
public Integer add(Integer a, Integer b) {
return a + b;
}
};

Script Code:

try {
// Call the com.apidog.utils.Utils.add (Integer, Integer) function const jarResult = pm.execute in com.apidog.Utils.jar ('com.apidog.utils.jar', [3,5], {className:' com.apidog.utils.Utils',method: 'add',paramTypes: [' Integer', 'Integer'],})
const jarResult = pm.execute('cn.apifox.utils.jar', [3, 5], {
className: 'cn.apifox.utils.Utils',
method: 'add',
paramTypes: ['Integer', 'Integer'],
})
console.log(jarResult); // The execution result is "8"
} catch (e) {
console.error(e.message);
}

FAQ

1. What should you do when different external systems return strings with different line breaks?

  • You can remove spaces and lines from the output.