Manual

iOS Packet-Capture Scripts for Request and Response Automation

When a simple rewrite is not enough, NetPeek scripts can run JavaScript before requests, after responses, or on a schedule, while constants keep reusable environment URLs, tokens, and other values in one place.

5Scripts

When rules aren't enough, use JavaScript to handle matched requests more flexibly: read/rewrite request & response, gunzip bodies, compute values conditionally. Scripts are scoped by host and run in the tunnel, like rules.

  1. Tap + in the script list, set a name and scope (host).
  2. Write your logic in the editor (helpers like $utils provide gzip/gunzip etc.).
  3. Save & enable, then re-send the request to see the effect.
Open scripts New script

7Constants

Store frequently-used values (a mock JSON, a token, a test URL) as constants and reference them in rules with ${constantId} — change one place, update everywhere.

Open constants New constant
Simple-mode Mock already assembles the response for you, so you rarely need a constant by hand; constants shine when reused across several expert-mode rules.

Reach for a script only when rules run out

Rewrite rules cover static substitution. Scripts are for the cases that need a decision or a computation:

  • Change the response conditionally — for example only replace the body when a specific error code comes back.
  • Values that must be computed: timestamps, signatures, random IDs. Hard-coding them in a rule is meaningless.
  • Work on decompressed content: gunzip a response body, edit it, and let it be recompressed.
  • Chain across requests: carry a token from one response into the next request.

Where scripts run

  • Scripts are scoped by host and execute inside the tunnel, exactly like rewrite rules.
  • Helpers such as $utils cover common operations including gzip and gunzip.
  • They can run before a request, after a response, or on a schedule.
  • Constants keep environment URLs and tokens in one place, shared across scripts.

FAQ

When should I use a script?

Use scripts for branching, combined transformations, or scheduled automation. Prefer rewrite rules for straightforward header, body, or mock changes.

Do scripts run automatically?

A script runs only when that script is enabled and the global Scripts gate is on.

What are constants for?

Constants centralize reusable values such as API endpoints or tokens so rules and scripts can share them.

Related features