Skip to content

response templating

Bernard Niset edited this page Sep 12, 2022 · 2 revisions

(This documentation is adapted from the original documentation of WireMock.)

Response headers and bodies, as well as proxy URLs, can optionally be rendered using Handlebars templates. This enables attributes of the request to be used in generating the response e.g. to pass the value of a request ID header as a response header or render an identifier from part of the URL in the response body.

Enabling response templating

{
  "request": {
    "urlPath": "/templated"
  },
  "response": {
    "body": "{{request.path.[0]}}",
    "transformers": ["response-template"]
  }
}

Command line parameters can be used to enable templating when running Mimus Serve CLI.

Proxying

Templating also works when defining proxy URLs, e.g.

{
  "request": {
    "urlPath": "/templated"
  },
  "response": {
    "proxyBaseUrl": "{{request.headers.X-WM-Proxy-Url}}",
    "transformers": ["response-template"]
  }
}

Templated body file

The body file for a response can be selected dynamically by templating the file path:

{
  "request": {
    "urlPathPattern": "/static/.*",
    "method": "GET"
  },
  "response": {
    "status": 200,
    "bodyFileName": "files/{{request.pathSegments.[1]}}"
  }
}

The request model

The model of the request is supplied to the header and body templates. The following request attributes are available:

request.url - URL path and query

request.path - URL path

request.pathSegments.[<n>]- URL path segment (zero indexed) e.g. request.pathSegments.[2]

request.query.<key>- First value of a query parameter e.g. request.query.search

request.query.<key>.[<n>]- nth value of a query parameter (zero indexed) e.g. request.query.search.[5]

request.method- request method e.g. POST

request.host- hostname part of the URL e.g. my.example.com

request.port- port number e.g. 8080

request.scheme- protocol part of the URL e.g. https

request.baseUrl- URL up to the start of the path e.g. https://my.example.com:8080

request.headers.<key>- First value of a request header e.g. request.headers.X-Request-Id

request.headers.[<key>]- Header with awkward characters e.g. request.headers.[$?blah]

request.headers.<key>.[<n>]- nth value of a header (zero indexed) e.g. request.headers.ManyThings.[1]

request.body - Request body text (avoid for non-text bodies)

request.cookies.<key> - First value of a request cookie e.g. request.cookies.JSESSIONID

request.cookies.<key>.[<n>] - nth value of a request cookie e.g. request.cookies.JSESSIONID.[2]

Values that can be one or many

A number of HTTP elements (query parameters, form fields, headers) can be single or multiple valued. The template request model and built-in helpers attempt to make this easy to work with by wrapping these in a "list or single" type that returns the first (and often only) value when no index is specified, but also support index access.

For instance, given a request URL like /multi-query?things=1&things=2&things=3 I can extract the query data in the following ways:

{{request.query.things}} // Will return 1
{{request.query.things.0}} // Will return 1
{{request.query.things.first}} // Will return 1
{{request.query.things.1}} // Will return 2
{{request.query.things.[-1]}} // Will return 2
{{request.query.things.last}} // Will return 3