什么是Postman
请求前置脚本
获取并赋值子系统Token
// 获取当前环境请求协议
var request_protocol = pm.environment.get("request_protocol");
console.log("获取当前环境请求协议:" + request_protocol);
// 获取当前环境网关域名
var gateway_hostname = pm.environment.get("gateway_hostname");
console.log("获取当前环境网关域名:" + gateway_hostname);
// 获取当前环境用户服务
var gateway_service_user = pm.environment.get("gateway_service_user");
console.log("获取当前环境用户服务:" + gateway_service_user);
// 获取当前环境子系统授权路径
var auth_sub_system_token_path = pm.environment.get("auth_sub_system_token_path");
console.log("获取当前环境子系统授权路径:" + auth_sub_system_token_path);
// 拼接并获取子系统授权的请求地址
var getSubSystemTokenUrl = request_protocol + ‘://‘ + gateway_hostname + "/" + gateway_service_user + ‘/‘ + auth_sub_system_token_path;
console.log("拼接并获取子系统授权的请求地址:" + getSubSystemTokenUrl);
// 获取当前环境子系统密钥
var auth_sub_system_secret = pm.environment.get("auth_sub_system_secret");
console.log("获取当前环境子系统密钥:" + auth_sub_system_secret);
// 获取当前环境子系统身份标识
var auth_sub_system_service_id = pm.environment.get("auth_sub_system_service_id");
console.log("获取当前环境子系统身份标识:" + auth_sub_system_service_id);
// 定义Post请求方法及参数
const echoPostRequest = {
url: getSubSystemTokenUrl,
method: ‘POST‘,
header: [
"Content-Type: application/json",
"Accept: application/json"
],
body: {
mode: ‘raw‘,
raw: JSON.stringify({
"secret": auth_sub_system_secret,
"sid": auth_sub_system_service_id
})
}
};
// 发送自定义请求
pm.sendRequest(echoPostRequest, function (err, response) {
// 将返回结果中的Data赋值到环境变量
pm.environment.set("auth_token_subsystem", response.json().data);
});
获取并赋值APOS对外接口签名
// 获取当前环境请求协议
var request_protocol = pm.environment.get("request_protocol");
console.log("获取当前环境请求协议:" + request_protocol);
// 获取当前环境网关地址
var gateway_path = pm.environment.get("gateway_path");
console.log("获取当前环境网关地址:" + gateway_path);
// 获取当前环境对外服务
var gateway_service_public = pm.environment.get("gateway_service_public");
console.log("获取当前环境对外服务:" + gateway_service_public);
// 获取当前环境生成签名的路径
var auth_app_create_sign_path = pm.environment.get("auth_app_create_sign_path");
console.log("获取当前环境生成签名的路径:" + auth_app_create_sign_path);
// 拼接并获取对外授权签名的请求地址
var createSignUrl = "";
// 判断纯数字的正则表达式
var reg = /^[0-9]+.?[0-9]*$/;
// 如果网关服务名称是纯数字,即服务端口号,说明不走网关模式,即调式模式
if(reg.test(gateway_service_public))
{
createSignUrl = request_protocol + ‘://‘ + gateway_path + ":" + gateway_service_public + ‘/‘ + auth_app_create_sign_path;
}
else
{
createSignUrl = request_protocol + ‘://‘ + gateway_path + "/" + gateway_service_public + ‘/‘ + auth_app_create_sign_path;
}
console.log("拼接并获取对外授权签名的请求地址:" + createSignUrl);
// 获取当前环境应用密钥
var auth_app_secret = pm.environment.get("auth_app_secret");
console.log("获取当前环境应用密钥:" + auth_app_secret);
// 定义Post请求方法及参数
const echoPostRequest = {
url: createSignUrl,
method: ‘POST‘,
header: [
"Content-Type: multipart/form-data",
"Accept: application/json"
],
body: {
mode: ‘formdata‘,
formdata: [
{
key: "SignKey", value: auth_app_secret, disabled: false, description: { content:"", type:"text/plain" }
},
{
key: "Content", value: pm.request.body.raw, disabled: false, description: { content:"", type:"text/plain" }
}
]
}
};
// 发送自定义请求
pm.sendRequest(echoPostRequest, function (err, response) {
// 将返回结果中的Sign赋值到环境变量
pm.environment.set("request_sign", response.json().data.sign);
});
参考
温故知新,.Net Core遇见Postman(API Development),进阶分布式微服务高效调式