How to Use Apipost Pre‑Execution Scripts to Call External Programs

This guide explains how Apipost’s pre‑execution scripts can run before a request, allowing developers to write JavaScript functions, manipulate variables, modify headers, queries, bodies, and invoke external programs via the apt.execute API, with version requirements, usage syntax, and a complete code example.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Use Apipost Pre‑Execution Scripts to Call External Programs

Overview

Apipost is a collaborative tool for API development that integrates debugging, documentation generation, and automated testing, covering the entire API lifecycle. It allows users to create, debug, test APIs, generate high‑quality docs, and collaborate in real time, both online and offline.

Pre‑execution scripts run before a request is sent. They can be added by defining custom JavaScript code, and Apipost can invoke external programs through these scripts (JavaScript can call other languages via external programs or libraries).

What Pre‑Execution Scripts Can Do

Write JS functions for complex calculations.

Print variables.

Define, get, delete, or clear environment variables.

Define, get, delete, or clear global variables.

Access request parameters.

Dynamically add or remove a header parameter.

Dynamically add or remove a query parameter.

Dynamically add or remove a body parameter.

Send an HTTP request.

Note: Only Apipost versions ≥ 7.0.13 support script calls to external programs.

Usage Method

In a script, call the external program with: apt.execute(fileName, args) Parameters: fileName: String – absolute path of the external executable. args: Array<String> – array of string arguments passed to the program.

Return value: String – the console output of the executed program.

Calling Principle

The tool runs the external program via the command line; the system selects the appropriate interpreter based on the file extension and returns the program’s console output as a string.

Code Example

Pre‑execution script
try {
  var fileName = "E:/test.php";
  var args = {
    event: "Tinywan",
    room_id: 2,
    channel_id: 10086
  };
  // JSON.stringify needed when passing JSON data as argument
  const phpResultString = apt.execute(fileName, [JSON.stringify(args)]);
  // Parse JSON string result if needed
  const phpResult = JSON.parse(phpResultString);
  console.log("PHP 运行结果 ", phpResult);
} catch (e) {
  console.error(e.message);
}
test.php code
<?php
/** 
 * @author Tinywan(ShaoBo Wan)
 * @date 2024/6/21 20:14
 */
declare(strict_types=1);

$param = json_decode($argv[1]);
$result = [];
foreach ($param as $key => $value) {
    $result[$key] = $value;
}
echo json_encode($result) . PHP_EOL;

Console Execution Result

Post‑execution operation
Console output
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptAPI testingApipostexternal programpre-execution script
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.