Create dynamic responses with templating

Create dynamic responses for your mock server with Mockoon's templating system.

Get Mockoon latest version and start mocking!

Download

Mockoon's implements Dummy JSON library in order to create dynamic responses. This templating system is supported in response's body, headers, file content, and file path.

Available helpers

Dummy JSON offers lots of built-in helpers: repeat, int, float, date, time, title, firstName, lastName, company, latitude, longitude, domain, TLD, email, street, city, country, phone, color, hexColor, guid, ipv4, ipv6, lorem, lowercase, uppercase, etc. Please have a look at Dummy JSON documentation to learn how to use them.

In addition to these helpers, some custom ones have been added to Mockoon:

  • array 'item1' 'item2' 'item3': create an array from items (to be used in the next helpers oneOf, someOf)
  • oneOf (array 'item1' 'item2' 'item3'): select a random item in the array passed in parameters
  • someOf (array 'item1' 'item2' 'item3') x y: select x to y random items in the array passed in parameters
  • #switch ... #case ... #default: select content depending on a variable, like a normal switch :) (see example below)

Mockoon also supports the following helpers which can gather request information:

  • body 'path' 'default value': get a path from a request body JSON. Path has the following form: key.0.key.5.key (syntax is based on NPM object-path package)
  • urlParam 'paramName1': get a param from the URL /:paramName1/:paramName2
  • queryParam 'param1' 'default value': get a param from the query string ?param1=xxx&param2=yyy or a default value if param is not present
  • header 'Header-Name' 'default value': get content from any request header or a default value if header is not present
  • hostname: get request hostname
  • ip: get request IP address
  • method : get request method (GET, PUT, POST, etc.)

Body and file content templating

Templating will work in the body textarea without consideration for the Content-Type that has been defined. It will also work with files content for a limited set of MIME types (application/json, text/html, text/css, text/csv, application/javascript, application/typescript, text/plain, application/xhtml+xml, application/xml).

Here is an example of what you can do with this templating system:

{
"userId": "{{urlParam 'id'}}",
"name": "{{queryParam 'name' 'John'}}",
"lang": "{{{header 'Accept-Language' 'en'}}}",
"elementTitle": "{{body 'elements.0.title' 'default'}}",
"ip": "{{ip}}",
"method": "{{method}}",
"hostname": "{{hostname}}",
"friends": [
{{#repeat 2}}
{
"id": {{@index}},
"name": "{{firstName}} {{lastName}}"
}
{{/repeat}}
],
"oneItem": "{{oneOf (array 'item1' 'item2' 'item3')}}",
"someItems": "{{someOf (array 'item1' 'item2' 'item3') 1 2}}",
"userName":
{{#switch (urlParam 'id')}}
{{#case "1"}}"John"{{/case}}
{{#case "2"}}"Jack"{{/case}}
{{#default}}"Peter"{{/default}}
{{/switch}}
}

Body response templating

And the response you can get with the following request:

GET /user/123456?name=john
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7
Body:
{
"element": [{"title": "My title"}]
}


Response:
{
"userId": "5",
"name": "john",
"lang": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
"elementTitle": "My title",
"ip": "::1",
"method": "GET",
"hostname": "localhost",
"friends": [
{
"id": 0,
"name": "Stephen Bradbury"
},
{
"id": 1,
"name": "Warren Caffey"
}
],
"oneItem":"item1",
"someItems":"item2",
"userName":"John"
}

This system is flexible enough so you can generate a lot of different contents like CSV files:

firstname,lastname,countryCode
{{#repeat 10}}
{{firstName}},{{lastName}},{{countryCode}}
{{/repeat}}


Response:
firstname,lastname,countryCode
Max,Magby,AZ
Stan,Muldoon,HM
Drew,Rebelo,CY
Cory,Neal,BG
Grace,Whitson,CY
Haydee,Backer,ET
Erik,Friedrich,MX
Stephen,Paquette,PH
Neida,Durrett,PN
Vaughn,Neal,MO

File input templating

The file input field in each route also supports templating with the same helpers. This allows you to dynamically serve files depending on the request parameters, like urlParam or any other helper. Example:

If you have a set of files named ./file1.json, ./file2.json, etc and a route named /:id you can use the urlParam helper in the file input: c:/.../file{{urlParam 'id'}}.json.

If you call this route with an id /1, ./file1.json will be sent.

File input templating

Headers templating

Finally, templating helpers are also supported in headers values (both route headers and environment headers):

Headers value templating

Join Mockoon's mailing list to receive latest updates