前言

Postman 脚本示例

  • Pre-request Script

    if (!pm.collectionVariables.has("forgeJS")) {
      pm.sendRequest("https://gitee.com/qbitoy/public/raw/master/forge_rsa_min.js", function (err, res) {
          if (err) {
              console.log(err);
          } else {
              pm.collectionVariables.set("forgeJS", res.text());
          }
      })
    } else {
      var moment = require('moment');
      eval(pm.collectionVariables.get("forgeJS"));   // 生成 forge 变量
      const prikey = pm.collectionVariables.get("prikey");
      console.log("prikey:" + prikey);
      var priv = forge.pki.privateKeyFromPem(prikey);
      const md = forge.md.sha256.create();
      var ts = moment().valueOf();
      pm.collectionVariables.set("ts", ts);
      console.log("ts:" + ts);
      var message = "/subject/eduSubject-" + ts;
      console.log("message:" + message);
      md.update(message);
      const data = md.digest().bytes();
      const sign = forge.util.encode64(priv.sign(md));
      pm.collectionVariables.set("sign", sign);
      console.log("sign:", sign);
    }

Postman 内置库

本文出自 qbit snap

qbit
268 声望279 粉丝