proxyman 通过 writeToFile 将响应结果写到本地磁盘

37 min read
// Addons List: https://docs.proxyman.io/scripting/addons
const { sayHello } = require("@addons/HelloWorld.js");

/// This func is called if the Request Checkbox is Enabled. You can modify the Request Data here before the request hits to the server
/// e.g. Add/Update/Remove: host, scheme, port, path, headers, queries, comment, color and body (json, form, plain-text, base64 encoded string)
///
/// Action Menu -> Import File to import a JSON file and use in the script. Or Import directly with request.bodyFilePath = "~/Desktop/myfile.json" (Applied for Response too)
/// Use global object `sharedState` to share data between Requests/Response from different scripts (e.g. sharedState.data = "My-Data")
///
async function onRequest(context, url, request) {
  // console.log(request);
  console.log(url);

  // Update or Add new headers
  // request.headers["X-New-Headers"] = "My-Value";

  // Update or Add new queries
  // request.queries["name"] = "Proxyman";

  // Body
  // var body = request.body;
  // body["new-key"] = "new-value"
  // request.body = body;

  // Done
  return request;
}

/// This func is called if the Response Checkbox is Enabled. You can modify the Response Data here before it goes to the client
/// e.g. Add/Update/Remove: headers, statusCode, comment, color and body (json, plain-text, base64 encoded string)
///
async function onResponse(context, url, request, response) {
  // console.log(response);

  // Update or Add new headers
  response.headers["Content-Type"] = "application/json";

  // Update status Code
  // response.statusCode = 500;

  // Update Body
  // var body = response.body;
  // body["new-key"] = "Proxyman";
  // response.body = body;

  // Or map a local file as a body
  // response.bodyFilePath = "~/Desktop/myfile.json"

  // Done
const data = response.body.data.node.messagesConnection.edges; 
//   console.log(data)
const items =  data.filter(i=>i.node.author !=="human")

items.forEach(item=>{
  let path = `/Users/pan/Desktop/md/${item.node.messageId}`;
   writeToFile(item.node.text, `/Users/pan/Desktop/md/${item.node.messageId}.md`);
})

  return response;
}

Scripting 功能的一些使用场景:

  1. 自动修改请求和响应:使用脚本可以自动修改请求和响应中的数据,例如添加、删除、替换请求头、请求体、响应头、响应体等数据。
  2. 自动化测试:使用脚本可以模拟用户的行为,自动化测试应用程序的功能和性能。
  3. 数据分析和统计:使用脚本可以分析请求和响应数据,统计应用程序的使用情况和性能指标。
  4. 数据转换和格式化:使用脚本可以将请求和响应数据转换为其他格式,例如 JSON、XML、CSV 等格式。
  5. 数据持久化:使用脚本可以将请求和响应数据保存到本地文件或数据库中,以便后续分析和处理。