-
Notifications
You must be signed in to change notification settings - Fork 654
Script
wanghongenpin edited this page May 6, 2024
·
4 revisions
ProxyPin Provides a scripting feature where developers can write JS code to flexibly manipulate requests/responses
function onRequest(context, request)
This function is called before the request reaches the server, where you can modify the request data
For specific parameter formats, see > Parameter Definition
async function onRequest(context, request) {
console.log(request.url);
//URL Parameter
request.queries["name"] = "value";
//Update or add Header
request.headers["X-New-Headers"] = "My-Value";
delete request.headers["Key-Need-Delete"];
//Update Body Use the fetch API request interface. For specific documents, you can search for fetch API.
//request.body = await fetch('https://www.baidu.com/').then(response => response.text());
//Shared parameters are taken out later during onResponse
context["name"] = "hello";
return request;
}
REQUEST INTERRUPT If you need to interrupt a request, the result of the onRequest function returns None!
function onResponse(context, request, response)
This function is called before the response data is sent to the client, where you can modify the response data
async function onResponse(context, request, response) {
//Update or add Header
//response.headers["Name"] = context["name"];
// Update status Code
response.statusCode = 500;
//var body = JSON.parse(response.body);
//body['key'] = "value";
//response.body = JSON.stringify(body);
return response;
}
//context
{
"os": "macos",
"scriptName": "Your Script Name",
"deviceId": "deviceId",
"session": {} //Runtime session object, parameters can be passed in different requests
}
//request
{
"method": "<String> HTTP Method. for example: GET, POST, ...",
"host": "<String> domain name. for example: www.baidu.com, localhost, ...",
"path": "<String>: URL Path. for example: /v1/api",
"queries": "<Map<String, String>> JS Dictionary object URL parameters",
"headers": "<Map<String, String>> JS All Header key values of the dictionary object",
"body": "<String> Request body string type json format conversion object needs to be called JSON.parse(request.body)",
}
//response
{
"statusCode": 200,
"headers": "<Map<String, String>> JS All Header key values of the dictionary object",
"body": "<String> Response body string type json format conversion object needs to be called JSON.parse(request.body)",
}
Fetch API Use reference documentation
https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch
https://www.ruanyifeng.com/blog/2020/12/fetch-tutorial.html