Returning dynamic responses using Handlebars templates
{
and }
). In the simplest case a helper can
simply output the value of a variable in the model:
name=value
. The following helper has a single
positional parameter followed by a parameter named format
:
tag
has the value <html>
.
The template
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.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]
request.body
- Request body text (avoid for non-text bodies)
request.id
- A random UUID assigned by WireMock Cloud to every request
request.bodyAsBase64
- the Base64 representation of the request body.
request.multipart
- if the request is a multipart request (boolean).
request.parts
- the individual parts of a multipart request are exposed via the template
model. Each part can be referenced by its name and exposes a number of properties in the template model. For example,
a multipart request with a name of text
has the following properties available:
request.parts.text.binary
- if the part is a binary type.request.parts.text.headers.<key>
- first value of a part header - request.parts.text.headers.content-type
request.parts.text.body
- part body as text.request.parts.text.bodyAsBase64
- part body as base64./multi-query?things=1&things=2&things=3
I can extract the query data in the following ways:
Note
When using the eq
helper with one-or-many values, it is necessary to use the indexed form, even if only one value is present.
The reason for this is that the non-indexed form returns the wrapper type and not a String, and will therefore fail any comparison
with another String value.