Random data generators
This functionality is only available on paid plans. Check our plans here.
Similarly to variables, you can use the $tweak. namespace to use chancejs random generators. This enables a wide variety of mock generation capabilities.
We plugged in the entire chancejs API surface in the $tweak. namespace that enables a myriad of use cases! You can literally pick any method directly from the chancejs API!
This feature is available for:
- Response payload
- Response hook
- Header cells, name and value, on every rule type including
headers only rules. An
X-Request-Id: $tweak.guidrow is one line of table instead of a hook. On Mock and Modify rules the value is generated per request; on the rule types the browser applies itself it is generated when the rule is installed, so it changes when you edit the rule rather than on every call.
A url expression takes variables but not generators. A generator gives a new value every time it runs, and a url is a matcher: a generated one would match nothing twice.
Variables allow you specify values to reuse across all your rules, without having to repeat that value more than necessary, making your rules cleaner and quicker to modify.
As an example let's look at how to generate a random country and city for a mocked response.
{
"location": {
"country": "$tweak.country",
"city": "$tweak.city"
}
}
In this particular request, tweak renders the following JSON.
{
"location": {
"country": "US",
"city": "Rihifkac"
}
}
Data generators are a powerful feature to generate random mock data when combined with response hooks. In response hooks you have unlimited programmatic access to an instance of our internal data generator through context.
Types​
tweak will respect the data type returned by the data generation function. If a given random data generator returns a numerical value, then tweak will render a numerical value. For example the following JSON:
{
"name": "Joe",
"age": "$tweak.age"
}
Will yield the following result:
{
"name": "Joe",
"age": 44
}
Need something else? Request a feature