Response Templating
Response templating allows you to create dynamic responses that include data from the incoming request, generate random values, or compute values at response time.
Template Syntax
Section titled “Template Syntax”Templates use double curly braces: {{expression}}
{ "response": { "body": { "message": "Hello, {{request.query.name}}" } }}Request: GET /api/greet?name=Alice
Response: {"message": "Hello, Alice"}
Request Data
Section titled “Request Data”Access various parts of the incoming request.
Path Parameters
Section titled “Path Parameters”{ "matcher": { "path": "/api/users/{id}" }, "response": { "body": { "id": "{{request.pathParam.id}}", "url": "/api/users/{{request.pathParam.id}}" } }}Query Parameters
Section titled “Query Parameters”{ "response": { "body": { "page": "{{request.query.page}}", "limit": "{{request.query.limit}}" } }}Headers
Section titled “Headers”{ "response": { "body": { "userAgent": "{{request.header.User-Agent}}", "correlationId": "{{request.header.X-Correlation-ID}}" } }}Request Body
Section titled “Request Body”Access parsed request body (JSON):
{ "response": { "body": { "received": { "username": "{{request.body.username}}", "email": "{{request.body.email}}" }, "status": "created" } }}Nested access:
{ "response": { "body": { "city": "{{request.body.address.city}}", "firstItem": "{{request.body.items[0].name}}" } }}Request Metadata
Section titled “Request Metadata”{ "response": { "body": { "method": "{{request.method}}", "path": "{{request.path}}", "fullUrl": "{{request.url}}" } }}Built-in Functions
Section titled “Built-in Functions”Timestamps
Section titled “Timestamps”{ "response": { "body": { "timestamp": "{{now}}", "isoDate": "{{now.iso}}", "unixTimestamp": "{{timestamp}}" } }}Output:
{ "timestamp": "2024-01-15T10:30:00Z", "isoDate": "2024-01-15T10:30:00Z", "unixTimestamp": 1705315800}Random Values
Section titled “Random Values”{ "response": { "body": { "id": "{{uuid}}", "randomFloat": "{{random.float 0 1}}" } }}String Functions
Section titled “String Functions”{ "response": { "body": { "upper": "{{upper request.body.name}}", "lower": "{{lower request.body.email}}", "trim": "{{trim request.body.input}}" } }}Conditional Logic
Section titled “Conditional Logic”If/Else
Section titled “If/Else”{ "response": { "body": { "status": "{{if request.query.premium}}premium{{else}}free{{end}}" } }}Default Values
Section titled “Default Values”{ "response": { "body": { "page": "{{default request.query.page \"1\"}}", "limit": "{{default request.query.limit \"10\"}}" } }}Loops and Arrays
Section titled “Loops and Arrays”Repeating Elements
Section titled “Repeating Elements”{ "response": { "body": { "items": "{{repeat 5 '{\"id\": {{index}}, \"name\": \"Item {{index}}\"}'}}" } }}Array Length
Section titled “Array Length”{ "response": { "body": { "itemCount": "{{len request.body.items}}" } }}Response Headers
Section titled “Response Headers”Templates work in headers too:
{ "response": { "headers": { "X-Request-ID": "{{uuid}}", "X-Correlation-ID": "{{request.header.X-Correlation-ID}}", "Location": "/api/users/{{request.body.id}}" } }}Escaping
Section titled “Escaping”Literal Braces
Section titled “Literal Braces”To output literal {{, escape with backslash:
{ "body": "Template syntax uses \\{{expression}}"}JSON in Templates
Section titled “JSON in Templates”When embedding JSON, ensure proper escaping:
{ "response": { "body": "{{jsonEncode request.body}}" }}Complete Example
Section titled “Complete Example”{ "matcher": { "method": "POST", "path": "/api/orders" }, "response": { "statusCode": 201, "headers": { "Content-Type": "application/json", "Location": "/api/orders/{{uuid}}", "X-Request-ID": "{{request.header.X-Request-ID}}" }, "body": { "id": "{{uuid}}", "status": "pending", "customer": { "name": "{{request.body.customer.name}}", "email": "{{lower request.body.customer.email}}" }, "items": "{{request.body.items}}", "itemCount": "{{len request.body.items}}", "total": "{{request.body.total}}", "createdAt": "{{now.iso}}", "estimatedDelivery": "{{now.addDays 3}}" } }}Request:
curl -X POST http://localhost:4280/api/orders \ -H "Content-Type: application/json" \ -H "X-Request-ID: req-123" \ -d '{ "customer": {"name": "Alice", "email": "ALICE@EXAMPLE.COM"}, "items": [{"sku": "A1", "qty": 2}], "total": 49.99 }'Response:
{ "id": "abc123", "status": "pending", "customer": { "name": "Alice", "email": "alice@example.com" }, "items": [{"sku": "A1", "qty": 2}], "itemCount": 1, "total": 49.99, "createdAt": "2024-01-15T10:30:00Z", "estimatedDelivery": "2024-01-18T10:30:00Z"}Template Reference
Section titled “Template Reference”| Expression | Description |
|---|---|
{{request.method}} | HTTP method |
{{request.path}} | Request path |
{{request.pathParam.name}} | Path parameter |
{{request.query.name}} | Query parameter |
{{request.header.Name}} | Request header |
{{request.body}} | Full request body |
{{request.body.field}} | Body field |
{{now}} | Current ISO timestamp |
{{timestamp}} | Unix timestamp (seconds) |
{{uuid}} | Random UUID |
{{upper value}} | Uppercase string |
{{lower value}} | Lowercase string |
{{default value fallback}} | Default if empty |
{{len array}} | Array length |
Next Steps
Section titled “Next Steps”- Request Matching - Matching rules
- Stateful Mocking - CRUD simulation
- Configuration Reference - Full schema