Snippet Code

The collection of useful snippet code for the Scripting tool

1. What's it?

A collection of snippet Javascript code for the Scripting Tool.

Could not find the Snippet Code you are looking for?

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

2. Common on Request and Response

3. Addons

4. Regex

5. Import / Export Files

6. Miscellaneous

7. Encryption/Decryption

8. Map Remote with Scripting

9. Make Async/await HTTP Request (macOS)

9.1. Make Async/await HTTP Request (Windows/Linux)

10. URL and URLSearchParams

11. Access Environment Variables

Websocket

HTTP Header

Add/Update: Request or Response Header

Delete: Request or Response Header

Request Query

Add/Update

Delete

URLEncoded Form Body

Add/Update

Delete

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

HTTP to HTTPS

HTTPS to HTTP

Change HTTP Request Method

Change Response HTTP Status Code

JSON Body

On Request

On Response

Map a local file to Response's Body like Map Local Tool (Proxyman 2.25.0+)

It's a convenient way to directly set a local file to a Body by using bodyFilePath property

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

Map Local with GraphQL

Read more at https://github.com/ProxymanApp/Proxyman/issues/412#issuecomment-697101594

Use ArrayBuffer in Request/Response Body

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.

Preserve the Host

Comment & Highlight with color

Use comment or color to highlight on the main table view.

Use Scripting for Color and Comment

Import Files with require()

JSON

  1. Prepare a JSON file and save it to your Desktop

2. More -> Import JSON or Other Files. Then selecting your file

3. Proxyman will add the import code to the top of the script

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

Binary File

  1. Follow the same above instruction (Select your Binary File)

  2. Set it as a body

Text-Based File

  1. Follow the same above instruction (Select your Binary File)

  2. Set it as a body

Import File without using the "Import Tool"

From Proxyman 2.24.0+, you can import any files without using the "Import File".

  • 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.

Use as Mock API

You can use Scripting as a Mock API by following this guideline.

Using Addons

Full list of built-in addons

Use Base64 Addon

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

Use UUID-v4 Addon

Deflate/Inflate and GZip/UnGZip

Deflate/inflate

GZip/UnGZip

JWT Decode

Logging

Regex

Only number

Regex to get the Scheme, Host, Port, Path, and Query of the URL

Miscellaneous

By-pass CORS

Inject Header to Request / Response

Response delay with sleep() function

the It's useful for simulating the "Slow Network" on a particular Request or Response. You can check out the Network Conditions tool for GUI.

  • macOS

  • Windows/Linux

AES Encryption/Decryption

DES Encryption/Decryption

Write / Export to a local file

  • Override Mode

  • Append Mode (Only for Proxyman 3.6.2+)

Check if the first exist

  • Available: Proxyman macOS 5.4.0+

Read a file

  • If it's text-based -> Return a String

  • Otherwise -> Return Uint8Array

  • Available: Proxyman macOS 5.4.0+

Map Remotes

Map v1 to v2 endpoints

Map localhost to production

Map production to localhost

Make async/await HTTP Request (macOS)

This feature `$http` is available on the macOS version. To use on Windows, please check the next part.

GET Request with Query

POST Request with JSON Body

POST Request with application/x-www-form-urlencoded body

PUT / PATCH / DELETE Request

Make async/await HTTP Request (Windows/Linux)

Windows/Linux ships with a built-in Axios library. You can easily make HTTP(s) requests with `Axios` syntax.

Abort the request/response like Block List Tool

  • Only available for Proxyman 3.11.0 and later

Abort the request

Abort the response

URL and URLSearchParams

From Proxyman 4.13.0 or later, URL and URLSearchParams are natively supported.

Reload System Environment Variables

Proxyman macOS 4.15.0 or later.

Make sure we enable the permission first, in the More Button -> Environment Variables -> Allow all scripts to read env.

Change Websocket URL, Requests/Response Header

  • Supported from macOS 6.2.0 or later

  • ❌ Can NOT modify the Websocket Message. Only URL and Headers are supported.

Map from localhost to production

  • Rule ws://proxyman.debug:3000

Production to localhost

  • Rule wss://echo.websocket.org

Last updated