Documentation

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 or application/x-www-form-urlencoded.

  • The path takes the following form key.0.key.5.key. The syntax is based on NPM object-path package.
  • For both JSON and form params bodies, 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

{{body}}
{{body 'path.to.property'}}
{{body 'path' 'default value'}}
{{body 'path' 'default value' true}}

queryParam

Get the value at a given path from the request's query string. Complex query strings with arrays and objects are supported.

  • The path takes the following form key.0.key.5.key. The syntax is based on NPM object-path package.
  • 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

{{queryParam}}
{{queryParam 'path.to.property'}}
{{queryParam 'path' 'default value'}}
{{queryParam 'path' 'default value' true}}

urlParam

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

Arguments (ordered)TypeDescription
0stringRoute parameter name

Examples

{{urlParam 'paramName1'}}

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

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

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

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

hostname

Returns the request hostname.

Examples

{{hostname}}

ip

Returns the request IP address.

Examples

{{ip}}

method

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

Examples

{{method}}

baseUrl

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

Examples

{{baseUrl}}
Feedback? Questions? We are always here to help.
Contact us