Snippet Code
The collection of useful snippet code for the Scripting tool
A collection of snippet Javascript code for the Scripting Tool.
Could not find the Snippet Code you are looking for?
function onRequest(context, url, request) {
// Add or update Request Header
request.headers["X-Proxyman-Key"] = "My-Value";
request.headers.name = "Proxyman";
return request;
}
function onResponse(context, url, request, response) {
// Add or update Response Header
response.headers["X-Proxyman-Key"] = "My-Value";
response.headers.id = 100;
return response;
}
function onRequest(context, url, request) {
delete request.headers["X-Proxyman-Key"];
return request;
}
function onResponse(context, url, request, response) {
delete response.headers["X-Proxyman-Key"];
return response;
}
function onRequest(context, url, request) {
request.queries["name"] = "Proxyman";
request.queries["platform"] = "macOS";
// => http://proxyman.io?name=Proxyman&platform=macOS
return request;
}
function onRequest(context, url, request) {
delete request.queries["name"];
delete request.queries["platform"];
return request;
}
function onRequest(context, url, request) {
// Make sure the Response Header is application/x-www-form-urlencoded
// Content-Type: application/x-www-form-urlencoded
var formBody = request.body;
formBody["name"] = "Proxyman";
formBody["flatform"] = "macOS";
request.body = formBody; // => name=Proxyman&platform=macOS
return request;
}
function onRequest(context, url, request) {
// Content-Type: application/x-www-form-urlencoded
var formBody = request.body;
delete formBody["name"];
delete formBody["flatform"];
request.body = formBody;
return request;
}
function onRequest(context, url, request) {
request.scheme = "http";
request.host = "proxyman.dev";
request.port = 9090;
request.path = "v1/data/user";
return request;
}
function onRequest(context, url, request) {
request.scheme = "http";
request.port = 80; // Don't forget to override the port
return request;
}
function onRequest(context, url, request) {
request.scheme = "https";
request.port = 443; // Don't forget to override the port
return request;
}
function onRequest(context, url, request) {
request.method = "POST";
return request;
}
function onResponse(context, url, request, response) {
response.statusCode = 404;
return response;
}
function onRequest(context, url, request) {
// Set JSON content
request.headers["Content-Type"] = "application/json";
// Get Request body
var jsonBody = request.body;
// Modify data
jsonBody["name"] = "Proxyman";
jsonBody["flatform"] = "macOS";
jsonBody["info"] = {
"website": "proxyman.io",
"region": "Earth"
};
// Set it again
request.body = jsonBody;
return request;
}
function onResponse(context, url, request, response) {
// Set JSON content
response.headers["Content-Type"] = "application/json";
// Get Request body
var jsonBody = response.body;
// Modify data
jsonBody["name"] = "Proxyman";
jsonBody["flatform"] = "macOS";
jsonBody["info"] = {
"website": "proxyman.io",
"region": "Earth"
};
// Set it again
response.body = jsonBody;
return response;
}
It's a convenient way to directly set a local file to a Body by using
bodyFilePath
property// For request
request.bodyFilePath = "~/Desktop/image.png";
// For response
response.bodyFilePath = "~/Desktop/image.png";
function onResponse(context, url, request, response) {
response.headers["Content-Type"] = "image/png";
response.bodyFilePath = "~/Desktop/image.png";
// Done
return response;
}
function onResponse(context, url, request, response) {
response.headers["Content-Type"] = "application/json";
response.bodyFilePath = "~/Desktop/my_response.json";
// Done
return response;
}
It's possible to use your JSON file and set it as a Request/Response's body
You can use set a different body for each matching endpoint by using the IF statement.
- 1.
- 2.Set Script Rule with wildcard or Regex pattern that can match many endpoints. E.g https://my-domain.com/v*
- 3.Use `includes()` to check whether or not the endpoint is matched
const file_1 = require("@users/myfile_1.json");
const file_2 = require("@users/myfile_2.json");
const file_3 = require("@users/myfile_3.json");
function onResponse(context, url, request, response) {
// Set JSON content
response.headers["Content-Type"] = "application/json";
// Check
if (url.includes("v1/data")) {
response.body = file_1
} else if (url.includes("v2/login")) {
response.body = file_2
} else if (url.includes("v3/user")) {
response.body = file_3
}
// Done
return response;
}
// Import your JSON file here
const file = require("@users/B02D96D5.default_message_32E64A5B.json");
function onRequest(context, url, request) {
// 1. Extract the queryName from the request
var queryName = request.body.query.match(/\S+/gi)[1].split('(').shift();
// 2. Save to sharedState
sharedState.queryName = queryName
// Done
return request;
}
function onResponse(context, url, request, response) {
// 3. Check if it's the request we need to map
if (sharedState.queryName == "user") {
// 4. Import the local file by Action Button -> Import
// Get the local JSON file and set it as a body (like Map Local)
response.headers["Content-Type"] = "application/json";
response.body = file;
}
// Done
return response;
}
Since Javascript doesn't have the Data object type, the Data Body will convert to Base64 Encoded String in Javascript. To pass Uint8Array, blob, or ArrayBuffer to the body, make sure you convert to Base64 Encoded String and set the ContentType to
application/octet-stream
Proxyman will convert Base64 Encoding to ArrayBuffer, so the client will receive the data properly.
// Import
const { btoa } = require('@addons/Base64.js');
function onResponse(context, url, request, response) {
// Construct the ArrayBuffer
const buffer = new ArrayBuffer(256)
const view = new Uint8Array(buffer)
for (let i = 0; i < view.length; i++) {
view[i] = i
}
// Convert ArrayBuffer to Base64String
var newBody = btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
// Set new body
response.body = newBody;
response.statusCode = 200
response.headers['Content-Type'] = 'application/octet-stream'
// Done
return response;
}
function onRequest(context, url, request) {
request.preserveHostHeader = true
return request
}
Use
comment
or color
to highlight on the main table view.function onRequest(context, url, request) {
request.comment = "It's a Request"
request.color = "red" // red, blue, yellow, purple, gray, green
return request
}
function onResponse(context, url, request, response) {
response.comment = "It's a Response"
response.color = "yellow" // red, blue, yellow, purple, gray, green
return response
}

Use Scripting for Color and Comment
- 1.Prepare a JSON file and save it to your Desktop
// myfile.json
{
"name": "Proxyman",
"platform": "macOS",
"info": {
"website": "proxyman.io",
"region": "Earth"
}
}
2. More -> Import JSON or Other Files. Then selecting your file

3. Proxyman will add the import code to the top of the script
// ~/Library/Application Support/com.proxyman.NSProxy/users/myfile.json
const file = require("@users/myfile.json");
function onResponse(context, url, request, response) {
// 1. Set header to JSON if need
response.headers["Content-Type"] = "application/json";
// 2. Set Body as a imported file
response.body = file;
return response;
}
The selected file is copied to ~/Library/Application\ Support/com.proxyman.NSProxy/users folder.
To support other format files, such as image, text, pdf. Make sure you have correct Header Content-Type
- 1.Follow the same above instruction (Select your Binary File)
- 2.Set it as a body
// ~/Library/Application Support/com.proxyman.NSProxy/users/myscreenshot.png
const file = require("@users/myscreenshot.png");
function onResponse(context, url, request, response) {
// Set header
response.headers["Content-Type"] = "image/png";
// Set Body
response.body = file;
return response;
}
- 1.Follow the same above instruction (Select your Binary File)
- 2.Set it as a body
// ~/Library/Application Support/com.proxyman.NSProxy/users/main.css
const file = require("@users/main.css");
function onResponse(context, url, request, response) {
// Set header
response.headers["Content-Type"] = "text/css";
// Set Body
response.body = file;
return response;
}
From Proxyman 2.24.0+, you can import any files without using the "Import File".
// Import your file from your Desktop folder
const file = require("~/Desktop/myfile.json");
function onResponse(context, url, request, response) {
// 1. Set header to JSON if need
response.headers["Content-Type"] = "application/json";
// 2. Set Body as a imported file
response.body = file;
return response;
}
- If the file has ".js" as an extension => Proxyman will execute it as a JS Script
- Otherwise, Proxyman will import it as normal
Only files that are imported by using the "Import Tool", are included when exporting the script to your colleague.
// Encode Base64
const { btoa } = require("@addons/Base64.js")
// Usage:
var text = "HelloWorld";
var encodedText = btoa(text);
// Decode Base64
const { atob } = require("@addons/Base64.js")
// Usage:
var text = atob("aGVsbG8=");
// Hash MD5
const { md5 } = require("@addons/MD5.js")
// Usage:
var hashedText = md5("Hello World");
// Hash SHA1
const { sha1 } = require("@addons/SHA1.js")
// Usage:
var hashedText = sha1("Hello World");
// Hash MD5
const { uuidv4 } = require("@addons/UUID.js")
// Usage:
var uuid = uuidv4();
const { inflate, deflate } = require("@addons/Pako.js")
// Compress
var input = "Hello World from Pako!!!"
var result = deflate(input);
console.log(result); // eJzzSM3JyVcIzy/KSVFIK8rPVQhIzM5XVFQEAGsMB/8=
// Decompress
var text = 'eJzzSM3JyVcIzy/KSVFIK8rPVQhIzM5XVFQEAGsMB/8=';
var rawText = inflate(text);
console.log(rawText); // Hello World from Pako!!!
const { ungzip, gzip } = require("@addons/Pako.js")
// Compress
var text = 'HelloWorld';
var result = gzip(text);
console.log(result); // H4sIAAAAAAAAA/NIzcnJD88vykkBAHkMd3cKAAAA
// Decompress
var text = 'H4sIAAAAAAAAA/NIzcnJD88vykkBAHkMd3cKAAAA';
var rawText = ungzip(text);
console.log(rawText); // HelloWorld
const { jwtDecode } = require('@addons/JWTDecode.js');
var text = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
var jSONObject = jwtDecode(text)
// Print the obj
const myObj = {};
console.log(myObj);
// Print type of the obj
console.log(typeof(myObj));
//Init Regex
var reg = /^-?\d*\.?\d*$/;
if (reg.test("123123123")) {
console.log("Matched");
}
function onRequest(context, url, request) {
console.log(url);
// Get each part
const regex = /^(https?):\/\/([^:\/\n]+)(?::(\d+))?([^#\n?]+)(?:\?([^#\n]+))?/;
const [, scheme, host, port, path, query] = url.match(regex);
// Assign again
request.scheme = scheme;
request.host = host;
if (port !== undefined) {
request.port = parseInt(port);
}
request.path = path;
// Log
console.log("----------");
console.log(scheme);
console.log(host);
console.log(port);
console.log(path);
// Done
return request;
}
function onResponse(context, url, request, response) {
// Allow all
response.headers["Access-Control-Allow-Origin"] = "*";
// Done
return response;
}
function onRequest(context, url, request) {
// add header
request.headers["My-Injected-Header"] = "Proxyman";
return request
}
function onResponse(context, url, request, response) {
// add header
response.headers["My-Injected-Header"] = "Proxyman";
return response;
}
It's useful for simulating the "Slow Network" on a particular Request or Response. You can check out the Network Conditions tool for GUI.
function onResponse(context, url, request, response) {
console.log("Start sleep");
// Sleep 5 seconds
sleep(5000);
// Done
return response;
}
const { encryptAES, decryptAES } = require("@addons/CryptoJS.js")
function onRequest(context, url, request) {
// Encrypt
var message = 'my message from Proxyman';
var password = 'secret key 123';
var ciphertext = encryptAES(message, password);
// Decrypt
var originalText = decryptAES(ciphertext, password);
// Done
return request;
}
const { encryptDES, decryptDES } = require("@addons/CryptoJS.js")
function onRequest(context, url, request) {
// Encrypt
var message = 'my message from Proxyman';
var password = 'secret key 123';
var ciphertext = encryptDES(message, password);
// Decrypt
var originalText = decryptDES(ciphertext, password);
// Done
return request;
}
- Override Mode
function onResponse(context, url, request, response) {
// Write to single file
writeToFile(response.body, "~/Desktop/body.json");