whistle.https-handle
v1.0.19
Published
whistle plugin - Modify http request and response
Downloads
4
Readme
##自用,修改自whistle.http-handle
修改内容:
1.去掉fixAndroid
2.增加拦截请求返回自定义响应内容后不发送真实请求
3.修改插件名称为whistle.https-handle,
4.更新版本号1.0.15->1.0.18->1.0.20默认规则
* https-handle://默认规则路径 js 文件为项目下的当前用户文件home文件夹下的 default.rules.js,例如:
windows系统: C:\Users\Administrator\default.rules.js
Linux系统: /home/default.rules.js
配置 whistle 规则 rules
匹配的域名 https-handle://自定义规则路径
www.baidu.com https-handle://E:\code\my\whistle.https-handle\baidu.js
www.douyin.com https-handle:///home/whistle.https-handle/douyin.js流程图

规则
reqConfig 真正请求时的数据配置
- url 请求的 url
- method 请求的方法
- headers 头信息
- query 查询字符串对象
- bodyType body 的类型 formData|form|json|text
- body 数据 string|object
resConfig 返回客户端时的数据配置
- statusCode 状态码
- headers 头信息
- body 数据 string|object|buffer object 时自动转为 json 字符串
next 传递给下一个请求的配置 传递false时为阻止请求或者响应
- reqConfig
- resConfig
- false
修改响应图片示例
const fs = require("fs");
const util = require("util");
const readFileAsync = util.promisify(fs.readFile);
module.exports = [
{
// 匹配的 url **为多个通配字符,*为单个统配字符
// 例如:https://www.baidu.com/action/**
url: "**", //匹配所有请求url
// 匹配的 method *表示所有
method: "get",
// 发送真正请求前 调用next()修改数据
beforeSendRequest(reqConfig, next) {
next(reqConfig);
},
// 返回响应给客户端前 调用next()修改数据
beforeSendResponse(reqConfig, resConfig, next) {
//判断响应的的类型是否是图片
if (resConfig.headers["content-type"].includes("image")) {
console.log("拦截图片响应...");
// 读取本地文件
const imagePath = "/storage/emulated/0/Pictures/cropedIMG_20230719_160944.jpg.png";
readFileAsync(imagePath)
.then(imageData => (resConfig.body = imageData)) // 将图片数据替换
.catch(error => console.error(error))
.finally(() => {
next(resConfig); // 把响应给客户端
});
} else {
next(resConfig); // 不是图片则不修改直接返回
}
}
}
];##阻止请求, 返回自定义响应
module.exports = [{
// 匹配的 url 可使用 ** ,* ,?匹配符
url: "http://www.baidu.com*",
// 匹配的 method
method: "get",
// 发送请求前 调用next()修改数据
beforeSendRequest(reqConfig, next) {
const customResponse = {
statusCode: 200,
headers: {
'server': 'nginx',
'content-type': 'application/json;charset=UTF-8',
'transfer-encoding': 'chunked',
'connection': 'keep-alive'
},
body: {"baidu": "hello"}
}
//console.log(customResponse)
reqConfig.resConfig = customResponse;
//构造自定义响应resConfig 挂到 reqConfig 上
next(reqConfig)
},
// 返回响应前 调用next()修改数据
beforeSendResponse(reqConfig, resConfig, next) {
next(resConfig)
}
}]
