Postman - 如何调用 shell 脚本并分配给 var?
Postman - How do I call a shell script and assign to a var?
我有一个简单的 shell 脚本,我想在后台 运行 并将其分配给 Postman 全局变量。关于如何使用 Postman 执行此操作的任何想法?
不使用全局变量,而是使用环境变量。
Postman 中的环境可以是 imported/exported 到可以通过外部脚本轻松修改的简单 json 文件。
通过外部脚本设置动态变量。
Postman 只能触发HTTP 请求,所以我们没有直接的方法可以执行.sh 脚本 等脚本。
但是你可以 运行 本地服务器(这里是 node js 服务器)并编写 node js 脚本来执行 shell 脚本 或其他可执行文件
现在让我们一步步来
第 1 步:定义您的任务
我有一个 PHP 脚本可以为我提供令牌。此令牌稍后将在 API 请求中使用。这个脚本看起来有点
像这样保存为 TokenGenerato.php
<?php
require "vendor/autoload.php";
class TokenGeneration {
public $appID= "50A454F3-EE11-4A4C-B708-965BC7640C08";
public $token = NULL;
public function __construct(){
//echo "\n URL: ".$this->url;
$this->token = $this->generateAppToken();
}
function generateAppToken(){
$message = $this->appID."~REQUEST".time();
//echo "\n message: ".$message;
$cryptor = new \RNCryptor\RNCryptor\Encryptor;
$base64Encrypted = $cryptor->encrypt($message, $this->appID);
//echo "\n token: ". $base64Encrypted;
return $base64Encrypted;
}
}
?>
第 2 步:获取您的令牌
我们将编写token.php程序从TokenGenerato.php
获取token
<?php
require "TokenGeneration.php";
$tokengen = new TokenGeneration();
echo $tokengen->token;
?>
我们可以通过
运行 这个 php 脚本
php token.php
输出:
AwFmHNkA1HdM1VHFadu89nE3xZuKO3pLQ7cHOrCj2x2WZoSt
第 3 步:创建 Node js 脚本。
你可以使用chile_process模块在node js中执行shell命令。 Tutorial on child_process
在token.js中使用以下代码创建节点js脚本
const http = require('http'), { exec } = require('child_process');
http.createServer((req, res) => {
// Give the path of your bat or exe file
exec('php token.php', (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(8000);
console.log('Server running at http://127.0.0.1:8000/');
接下来使用以下命令启动节点服务器
node token.js
第 4 步:Postman 的预请求脚本。
首先在邮递员中创建新的 API 请求,然后 select 预请求脚本并编写以下代码以在实际 API 调用发生之前发送 http 请求并保存响应在局部变量中。
pm.variables.set("password", "AwErFEinDqiOvXFx2wVgHvt+Rpo0jdoTH0D0QldS");
console.log("password: ", pm.variables.get("password"));
pm.sendRequest('http://127.0.0.1:8000', (err, response) => {
// This will have the output of your batch file
console.log(response.text());
pm.variables.set("token", response.text());
})
最后,准备使用token的header
token = {{token}}
我有一个简单的 shell 脚本,我想在后台 运行 并将其分配给 Postman 全局变量。关于如何使用 Postman 执行此操作的任何想法?
不使用全局变量,而是使用环境变量。
Postman 中的环境可以是 imported/exported 到可以通过外部脚本轻松修改的简单 json 文件。
通过外部脚本设置动态变量。
Postman 只能触发HTTP 请求,所以我们没有直接的方法可以执行.sh 脚本 等脚本。 但是你可以 运行 本地服务器(这里是 node js 服务器)并编写 node js 脚本来执行 shell 脚本 或其他可执行文件 现在让我们一步步来
第 1 步:定义您的任务
我有一个 PHP 脚本可以为我提供令牌。此令牌稍后将在 API 请求中使用。这个脚本看起来有点 像这样保存为 TokenGenerato.php
<?php
require "vendor/autoload.php";
class TokenGeneration {
public $appID= "50A454F3-EE11-4A4C-B708-965BC7640C08";
public $token = NULL;
public function __construct(){
//echo "\n URL: ".$this->url;
$this->token = $this->generateAppToken();
}
function generateAppToken(){
$message = $this->appID."~REQUEST".time();
//echo "\n message: ".$message;
$cryptor = new \RNCryptor\RNCryptor\Encryptor;
$base64Encrypted = $cryptor->encrypt($message, $this->appID);
//echo "\n token: ". $base64Encrypted;
return $base64Encrypted;
}
}
?>
第 2 步:获取您的令牌
我们将编写token.php程序从TokenGenerato.php
获取token<?php
require "TokenGeneration.php";
$tokengen = new TokenGeneration();
echo $tokengen->token;
?>
我们可以通过
运行 这个 php 脚本php token.php
输出:
AwFmHNkA1HdM1VHFadu89nE3xZuKO3pLQ7cHOrCj2x2WZoSt
第 3 步:创建 Node js 脚本。
你可以使用chile_process模块在node js中执行shell命令。 Tutorial on child_process
在token.js中使用以下代码创建节点js脚本
const http = require('http'), { exec } = require('child_process');
http.createServer((req, res) => {
// Give the path of your bat or exe file
exec('php token.php', (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(8000);
console.log('Server running at http://127.0.0.1:8000/');
接下来使用以下命令启动节点服务器
node token.js
第 4 步:Postman 的预请求脚本。
首先在邮递员中创建新的 API 请求,然后 select 预请求脚本并编写以下代码以在实际 API 调用发生之前发送 http 请求并保存响应在局部变量中。
pm.variables.set("password", "AwErFEinDqiOvXFx2wVgHvt+Rpo0jdoTH0D0QldS");
console.log("password: ", pm.variables.get("password"));
pm.sendRequest('http://127.0.0.1:8000', (err, response) => {
// This will have the output of your batch file
console.log(response.text());
pm.variables.set("token", response.text());
})
最后,准备使用token的header
token = {{token}}