postman 腳本編程入門

2019-11-12     鋒哥愛學習

簡介

postman提供了強大的腳本功能,你可以編寫預處理腳本,比如增加加密變量等。

scripts系統

首先,我們可能會用到引入第三方js類。 那麼postman默認提供了很多類,你可以直接require,然後可以調用對應的api

ajv v6.6.2

atob v2.1.2

btoa v1.2.1

chai v4.2.0

cheerio v0.22.0

crypto-js v3.1.9-1

csv-parse/lib/sync v1.2.4

lodash v4.17.11 (when used with require, the inbuilt _ object is for v3.10.1)

moment v2.22.2 (sans locales)

postman-collection v3.4.0

tv4 v1.3.0

uuid (the module loaded is a shim for original module)

xml2js v0.4.19

A number of NodeJS modules are also available to use in the sandbox:

path

assert

buffer

util

url

punycode

querystring

string-decoder

stream

timers

events

使用方式很簡單,require就行了。

實例代碼

 1var atob = require('atob'),
2 _ = require('lodash'),
3
4 arrayOfStrings,
5 base64Strings;
6
7arrayOfStrings = ['string1', 'string2'];
8
9base64Strings = _.map(arrayOfStrings, atob);
10
11console.log(base64Strings);

如圖所示,我們添加了一個pre-request 腳本, 它會在請求發起之前計算,這裡是計算一個時間戳

然後追加時間戳到請求參數中去

這樣我們就可以利用postman,完成一些複雜的任務,比如訪問鑒權加密的api接口,等等。

文章來源: https://twgreatdaily.com/zh-cn/oUBVzW4BMH2_cNUgov0A.html