Templating request helpers#


Mockoon offers the following helpers which can return information relative to the entering request:

body#

Get the value at a given path from the request body if the entering Content-Type is set to application/json, application/x-www-form-urlencoded, multipart/form-data, application/xml, application/soap+xml or text/xml. This helper is designed to retrieve data to be served in a response. To reuse the retrieved data with other helpers (each, if, etc.), use the bodyRaw helper below.

  • The path takes the following form key.0.key.5.key and is based on the object-path library. Properties containing dots are supported by escaping the dots: key.key\.with\.dot.
    Please note that XML bodies are parsed using xml-js package. Refer to this page or the package documentation for more information on how the XML is parsed and how to fetch specific properties.
    Please also note that multipart/form-data only supports fields. Uploaded files will be ignored.
  • Full objects or arrays can be retrieved by the helper.
  • The full request's raw body can also be fetched when the path is omitted ({{body}}) independently from the request's Content-Type.
  • If no value is present at the requested path, the default value will be used.
  • A third parameter (boolean) can be set to true to returns a stringified value even if it's a primitive.
Arguments (ordered)TypeDescription
0stringPath to the body property
1stringDefault value if property is not found
2booleanStringify primitive values

Examples

Copy
{{body}} {{body 'path.to.property'}} {{body 'deep.property\.with\.dot'}} {{body 'path' 'default value'}} {{body 'path' 'default value' true}}

bodyRaw#

Get the raw value at a given path from the request body if the entering Content-Type is set to application/json, application/x-www-form-urlencoded, multipart/form-data, application/xml, application/soap+xml or text/xml. This "raw" helper is designed to work with other helpers (each, if, etc.). To directly use the retrieved data in the response, use the body helper above.

  • The path takes the following form key.0.key.5.key and is based on the object-path library. Properties containing dots are supported by escaping the dots: key.key\.with\.dot.
    Please note that XML bodies are parsed using xml-js package. Refer to this page or the package documentation for more information on how the XML is parsed and how to fetch specific properties.
    Please also note that multipart/form-data only supports fields. Uploaded files will be ignored.
  • Full objects or arrays can be retrieved by the helper.
  • The full request's raw body can also be fetched when the path is omitted ({{bodyRaw}}) independently from the request's Content-Type.
  • If no value is present at the requested path, the default value will be used.
  • This helper allows the use of body within handlebars' helpers such as {{#each}} and {{#if}}.
Arguments (ordered)TypeDescription
0stringPath to the body property
1stringDefault value if property is not found

Examples

Copy
{{bodyRaw}} {{bodyRaw 'path.to.property'}} {{bodyRaw 'deep.property\.with\.dot'}} {{bodyRaw 'path' 'default value'}} {{#each (bodyRaw 'path.to.array.property' 'default value')}} value {{/each}} {{#if (bodyRaw 'path.to.boolean.property' 'default value')}} value {{/if}}

queryParam#

Get the value at a given path from the request's query string. Complex query strings with arrays and objects are supported. This helper is designed to retrieve data to be served in a response. To reuse the retrieved data with other helpers (each, if, etc.), use the queryParamRaw helper below.

  • The path takes the following form key.0.key.5.key. The syntax is based on NPM object-path package. Properties containing dots are supported by escaping the dots: key.key\.with\.dot.
  • Full objects or arrays can be retrieved by the helper.
  • The full query string object can also be fetched when the path is omitted ({{queryParam}}). It will be stringified and can be used in a JSON body for example.
  • If there is no value at the requested path, the default value will be used.
  • A third parameter (boolean) can be set to true to returns a stringified value even if it's a primitive.
Arguments (ordered)TypeDescription
0stringPath to the query param property
1stringDefault value if property is not found
2booleanStringify primitive values

Examples

Copy
{{queryParam}} {{queryParam 'path.to.property'}} {{queryParam 'deep.property\.with\.dot'}} {{queryParam 'path' 'default value'}} {{queryParam 'path' 'default value' true}}

queryParamRaw#

Get the raw value at a given path from the request's query string. Complex query strings with arrays and objects are supported. This "raw" helper is designed to work with other helpers (each, if, etc.). To directly use the retrieved data in the response, use the queryParam helper above.

  • The path takes the following form key.0.key.5.key. The syntax is based on NPM object-path package. Properties containing dots are supported by escaping the dots: key.key\.with\.dot.
  • Full objects or arrays can be retrieved by the helper.
  • The full query string object can also be fetched when the path is omitted ({{queryParamRaw}}).
  • If there is no value at the requested path, the default value will be used.
  • This helper allows the use of queryParam within handlebars' helpers such as {{#each}} and {{#if}}.
Arguments (ordered)TypeDescription
0stringPath to the query param property
1stringDefault value if property is not found

Examples

Copy
{{queryParamRaw}} {{queryParamRaw 'path.to.property'}} {{queryParamRaw 'deep.property\.with\.dot'}} {{queryParamRaw 'path' 'default value'}} {{#each (queryParamRaw 'path.to.array.query' 'default value')}} value {{/each}} {{#if (queryParamRaw 'path.to.boolean.query' 'default value')}} value {{/if}}

urlParam#

Get a named parameter from the route /:paramName1/:paramName2.

Arguments (ordered)TypeDescription
0stringRoute parameter name

Examples

Copy
{{urlParam 'paramName1'}}

cookie#

Get the content of a cookie or returns a default value if the cookie is not present.

Arguments (ordered)TypeDescription
0stringCookie name
1stringDefault value if cookie is not found

Examples

Copy
{{cookie 'cookie_name' 'default value'}}

header#

Get content from any request header or returns a default value if header is not present.

Arguments (ordered)TypeDescription
0stringHeader name
1stringDefault value if header is not found

Examples

Copy
{{header 'Header-Name' 'default value'}}

hostname#

Returns the request hostname.

Examples

Copy
{{hostname}}

ip#

Returns the request IP address.

Examples

Copy
{{ip}}

method#

Returns the request method (GET, PUT, POST, etc.).

Examples

Copy
{{method}}

baseUrl#

Returns the base URL of the request: protocol, host, port and API prefix.

Examples

Copy
{{baseUrl}}