IDEA HTTP Client 抽取处理脚本到公共 JS 文件

IDEA HTTP Client About 1,525 words

需求

RequestResponse的前置和后置处理脚本,抽取到外部js文件中,方便复用。

原始

< {%
    const timestamp = Date.now().toString();
    const nonce = Math.random().toString();
    const plaintext = timestamp + nonce;
    const signature = crypto.md5().updateWithText(plaintext).digest().toHex();
    client.log(`plaintext: ${plaintext}, signature: ${signature}`)
    client.log(request.body.tryGetSubstituted());
    request.variables.set("timestamp", timestamp);
    request.variables.set("nonce", nonce);
    request.variables.set("signature", signature);
%}
POST {{host}}/api/login
Content-Type: application/json
X-Ca-Timestamp: {{timestamp}}
X-Ca-Nonce: {{nonce}}
X-Ca-Signature: {{signature}}

抽取

命名为:header.js(任意名称皆可)

const timestamp = Date.now().toString();
const nonce = Math.random().toString();
const plaintext = timestamp + nonce;
const signature = crypto.md5().updateWithText(plaintext).digest().toHex();
client.log(`plaintext: ${plaintext}, signature: ${signature}`)
client.log(request.body.tryGetSubstituted());
request.variables.set("timestamp", timestamp);
request.variables.set("nonce", nonce);
request.variables.set("signature", signature);

前置处理修改为:< header.js

如有后置处理,可以修改为:> response.js

# @name Login
< header.js
POST {{host}}/api/login
Content-Type: application/json
X-Ca-Timestamp: {{timestamp}}
X-Ca-Nonce: {{nonce}}
X-Ca-Signature: {{signature}}

添加提示

打开设置,选择Languages & Frameworks,找到JavaScript,选择Libraries,勾选HTTP Pre-Request and Response Handler,点击OK

Preferences | Languages & Frameworks | JavaScript | Libraries
Views: 648 · Posted: 2023-05-31

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh