Scripting lets you modify proxied requests and responses using JavaScript code. This gives you the greatest amount of flexibility to implement any custom logic you need.

Scripts can be saved both globally in Proxygen app and in each project document. Toggle Use scripts saved in project to enable global or project specific scripts.

Add, remove and reorder scripts using the list on the left. You can quickly make duplicates of existing scripts.

Match

Execute scripts only for specific traffic by matching requests using protocol, method, host and URI fields. Leave these fields to default values to execute a script for all traffic.

Scripting

Each script has two callbacks

  • onRequest for outgoing requests
  • onResponse for incoming responses

Request editing

Examples of modifying a request in onRequest

/*
 * Called before request is sent to server.
 * Return modified request.
 *
 * context: { url, scheme, host, port, clientIpAddress, clientPort }
 * request: { method, path, query, headers, body, rawBody }
 */
function onRequest(context, request) {
	// log to console
	console.log("Request to", context.host, request.path);

	// change request method
	request.method = "POST";

	// change request path
	request.path = "/new/path";

	// add or replace value of query item
	request.query["key"] = "value";

	// delete query item
	delete request.query["key"];

	// add or replace header value
	request.headers["X-Custom"] = "value";

	// delete header value
	delete request.headers["X-Custom"];

	// set request body to JSON object
	request.body = { json: "data" };

	// return the modified request
	return request;

	// or return a new mock response
	return { statusCode: 200, headers: {}, body: "OK" };

	// or drop request
	return false;
}

Response editing

Examples of modifying a response in onResponse

/*
 * Called before response is returned to client.
 * Return modified response.
 *
 * context: { url, scheme, host, port, clientIpAddress, clientPort, serverIpAddress, serverPort }
 * request: { method, path, query, headers }
 * response: { statusCode, headers, body, rawBody }
 */
function onResponse(context, request, response) {
	// log to console
	console.log("Response", response.statusCode, context.url);

	// change status code
	response.statusCode = 200;

	// add or replace header value
	response.headers["X-Modified"] = "true";

	// set response body to JSON object
	response.body = { json: "data" };

	// return modified response
	return response;
}

Utilities

The following utilities are available to scripts.

 btoa(string) // encode string to Base64
 atob(base64String) // decode Base64 to string
 hexEncode(string) // encode string to hex
 hexDecode(hexString) // decode hex to string
 md5(string), sha1(string), sha256(string), sha512(string) // hash
 hmac(algorithm, key, data) // HMAC ("md5", "sha1", "sha256", "sha512")
 uuid() // generate random UUID
 readFile(path) // read text file as string or binary file as byte array
 writeFile(path, data) // write string or byte array to file
 fileExists(path) // check if file exists
 decodeJWT(token) // decode JWT token { header, payload }
 jsonToQuery(object) // convert object to query string
 queryToJson(string) // convert query string to object
 console.log(), console.info(), console.debug(), console.error()

Note that readFile and writeFile functions only have access to a folder you have selected in using Set Files Folder option. This is due to app sandboxing restrictions.