Snippet Code

1. What's it?

A collection of snippet Javascript code for the Scripting Tool.

Couldn't find the Snippet Code you're looking for?

Please open a ticket at https://github.com/ProxymanApp/Proxyman, we will get back and help you ⭐️

2. Common on Request and Response

3. Addons

4. Regex

5. Import Files

6. Miscellaneous

HTTP Header

Add/Update: Request or Response Header

request.headers["X-Proxyman-Key"] = "My-Value";
response.headers["X-Proxyman-Key"] = "My-Value";

Delete: Header

delete request.headers["X-Proxyman-Key"];
delete response.headers["X-Proxyman-Key"];

Request Query

Add/Update

request.queries["name"] = "Proxyman";
request.queries["platform"] = "macOS";
// => http://proxyman.io?name=Proxyman&platform=macOS

Delete

delete request.queries["name"];
delete request.queries["platform"];
// => http://proxyman.io

URLEncoded Form Body

Add/Update

// Content-Type: application/x-www-form-urlencoded
var formBody = request.body;
formBody["name"] = "Proxyman";
formBody["flatform"] = "macOS";
request.body = formBody;
// => name=Proxyman&platform=macOS

Delete

// Content-Type: application/x-www-form-urlencoded
var formBody = request.body;
delete formBody["name"];
delete formBody["flatform"];
request.body = formBody;

Change Request Destination (schema, host, port, path)

request.schema = "http";
request.host = "proxyman.dev";
request.port = 9090;
request.path = "v1/data/user";

Change HTTP Request Method

request.method = "POST";

Change Response HTTP Status Code

response.statusCode = 404;

JSON Body

Add/Update

// Content-Type: application/json or JSON Families
// Work fine with request or response
var jsonBody = request.body;
jsonBody["name"] = "Proxyman";
jsonBody["flatform"] = "macOS";
jsonBody["info"] = {
"website": "proxyman.io",
"region": "Earth"
};
request.body = jsonBody;
/*
{
"name": "Proxyman",
"platform": "macOS",
"info": {
"website": "proxyman.io",
"region": "Earth"
}
}
*/

Delete

// Content-Type: application/json or JSON Families
// Work fine with request or response
var jsonBody = request.body;
delete jsonBody["name"];
request.body = jsonBody;

Use JSON File as a Body of Request or Response

It's possible to use your JSON file and set it as a Request/Response's body

Please follow this tutorial

Use multiple JSON files

You can use set a different body for each matching endpoint by using the IF statement.

  1. Follow this tutorial to understand how to import a JSON file to the script

  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 Files

JSON

  1. Prepare a JSON file

// myfile.json
{
"name": "Proxyman",
"platform": "macOS",
"info": {
"website": "proxyman.io",
"region": "Earth"
}
}

2. Import to the Script by Import Tool

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) {
// Set header to JSON if need
response.headers["Content-Type"] = "application/json";
// Set Body
response.body = file;
return response;
}

The selected file is copied to ~/Library/Application\ Support/com.proxyman.NSProxy/users folder.

Support other format files, such as image, text, pdf. Make sure you have correct Header Content-Type

Binary File

  1. Import a binary file, a PNG for example: Action menu -> Import

  2. Set 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;
}

Text-Based File

  1. Import a binary file, a CSS for example: Action menu -> Import

  2. Set 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;
}

Using Addons

Full list of built-in addons

Use Base64 Addon

// 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=");

Use Hashing Addon (MD5, SHA1, SHA256, SHA512)

// 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");

Use UUID-v4 Addon

// Hash MD5
const { uuidv4 } = require("@addons/UUID.js")
// Usage:
var uuid = uuidv4();

Deflate/Inflate and GZip/UnGZip

Deflate/inflate

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!!!

GZip/UnGZip

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

JWT Decode

const { jwtDecode } = require('@addons/JWTDecode.js');
var text = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
var jSONObject = jwtDecode(text)

Logging

// Print the obj
const myObj = {};
console.log(myObj);
// Print type of the obj
console.log(typeof(myObj));

Regex

Only number

//Init Regex
var reg = /^-?\d*\.?\d*$/;
if (reg.test("123123123")) {
console.log("Matched");
}

Miscellaneous

By-pass CORS

function onResponse(context, url, request, response) {
// Allow all
response.headers["Access-Control-Allow-Origin"] = "*";
// Done
return response;
}

Inject Header to Request / 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;
}

Response delay with sleep() function

function onResponse(context, url, request, response) {
console.log("Start sleep");
// Sleep 5 seconds
sleep(5000);
// Done
return response;
}