External programs are code files saved under the "External Programs Directory", which can be java program archive files (.jar packages), or code source files of other programs. It supports files with extensions like:
java (.jar)
python (.py)
php (.php)
js (.js)
BeanShell (.bsh)
go (.go)
shell (.sh)
ruby (.rb)
lua (.lua)
External programs run outside the "sandbox environment", with access to operate other programs, files, and data on your computer, which poses some security risks. Users should verify the security of called programs.
You can quickly open the "External Program Directory" in the following way:When calling an external program, Apidog will start a subprocess and run the specified external program in it with command line execution. Finally the standard output (stdout) of the subprocess will be returned as the result of the call. In the whole calling process, the core logic is implemented by users in the external program, while Apidog mainly does the following 3 steps:
1.
Combine the command string based on the provided parameters
2.
Execute the command
3.
Return the result
Among them, the first step is key to understand the calling principles. Apidog uses the formula "command prefix + program path + parameter list" to concatenate the command.The "command prefix" is inferred from the file extension of the program file. The "program path" and "parameter list" are both provided by users while calling.For example: pm.execute('com.apidog.Base64EncodeDemo.jar', ['abc','bcd']), the actual executed command is java -jar "com.apidog.Base64EncodeDemo.jar" "abc" "bcd".The mapping between program file extensions and command prefixes:
argsstring[] Parameters. When calling specified methods in a jar package, JSON.stringify will be used for conversion. Except for that, non-string types will be implicitly converted to string.
optionsObject
commandstring The execution command of the external program, the first part of "command prefix" is the execution command. Optional, default value is inferred automatically (see "command prefix" table above), can be customized to any program.
cwdstring Working directory of the subprocess. Optional, default is "External Programs Directory".
envRecord<string, string> Environment variables of the subprocess. Optional, default is {}.
windowsEncodingstring Encoding used on Windows system. Optional, default is "cp936".
classNamestring Specify the class name to call in the jar package, e.g. "com.apidog.Utils". Optional, see Call specified methods in jar packages for details.
methodstring Specify the method name to call in the jar package, e.g. "add". Optional (required if className has value), see Call specified methods in jar packages for details.
paramTypesstring[] Specify the parameter types of the method to call in the jar package, e.g. ["int", "int"]. Optional, default is inferred automatically based on parameters, see Call specified methods in jar packages for details.
Return: Promise<string>
Usage of command parameter
By default Apidog uses python to execute .py files. If python3 is already installed on the computer, command can be specified as python3.
argsstring[] Parameters. When calling specified methods in a jar package, JSON.stringify will be used for conversion. Except for that, non-string types will be implicitly converted to string.
optionsObject
windowsEncodingstring Encoding used on Windows system. Optional, default is "cp936".
classNamestring Specify the class name to call in the jar package, e.g. "com.apidog.Utils". Optional, see Call specified methods in jar packages for details.
methodstring Specify the method name to call in the jar package, e.g. "add". Optional (required if className has value), see Call specified methods in jar packages for details.
paramTypesstring[] Specify the parameter types of the method to call in the jar package, e.g. ["int", "int"]. Optional, default is inferred automatically based on parameters, see Call specified methods in jar packages for details.
When executing a program, the executed command will be printed in the console (for reference only). If the result does not meet expectations, you can copy the command and paste it in Shell/CMD to debug.The console will also print the "standard output (stdout)" and "standard error output (stderr)" of the executed process. The stdout content (excluding the trailing newline character) will be the final result of the execution.
TIP
For historical reasons, pm.execute treats execution as failed when there is content in stderr. This causes some programs to fail when outputting warnings or error messages. pm.executeAsync changes to use the exit code of the process to determine if the execution failed.
Since the specified external program runs with command line execution, it can only get the passed in parameters through command line arguments.For example, in script pm.executeAsync('add.js', [2, 3]), the actual executed command is node add.js 2 3. To get the parameters in the external script add.js:
TIP
1.
Different programming languages have different ways to get command line arguments, please refer to corresponding language docs.
2.
The type of command line arguments is always string, need to convert based on actual types.
As mentioned above, Apidog uses the stdout content as the result of program execution. So printing content to stdout can return results.For example, in script const result = await pm.executeAsync('add.js', [2, 3]), the result can be returned by:
1.
Different programming languages have different ways to print to stdout, refer to corresponding language docs.
2.
The return type is string, need to convert based on actual types.
3.
The trailing newline character of the result will be trimmed.
Since pm.executeAsync uses exit code instead of stderr to determine success, stderr can be used to print debug information without affecting execution.For example:
TIP
1.
Only pm.executeAsync supports this way of printing debug info.
2.
Different programming languages have different ways to print to stderr, refer to corresponding docs.
Since the return value of pm.executeAsync is Promise type, execute cannot be directly changed to executeAsync. But you can use async/await to migrate with minimal changes.
TIP
Apidog version 2.3.24 or later(CLI version 1.2.38 or later) supports top-level await.
This feature requires Apidog version to be 2.1.39 or later. It only supports calling jars with reflection, not jars like Spring Boot using internal runtime reflection.
By default, calling a jar will invoke the main method in the Main class. If options.className is specified, it will override the default behavior and call the specified method in the jar instead.Calling specified methods in jars is different from other external programs. Apidog will use a built-in executor to find the method in the jar by reflection and call it. If the called method has a return value, it will be used as the final return value after converting to string. Otherwise, it works the same as other calls, using stdout content as return value.For example:
The actually executed command is:
Where <app-dist>/assets/JarExecuter-1.1.0-jar-with-dependencies.jar is the built-in executor, responsible for finding the method com.apidog.Test.combine(String,String) in the user program ./scripts/jar-1.0-SNAPSHOT.jar through reflection, and calling it with parameters (JSON string) "hello" and "world".
TIP
paramTypes is optional. If not specified, types will be inferred automatically based on parameters. Integers are inferred as "int", floats as "double", booleans as "boolean", strings as "String", arrays are inferred based on the first element, e.g. [3] is inferred as "int[]", [3.14] as "double[]", etc. If the inferred types do not match the actual parameter types of the called method, paramTypes needs to be specified manually. Supported values in paramTypes array: "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[]"So the paramTypes in the example above can be omitted: