mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-21 08:59:50 +01:00
Updated Response Templating (markdown)
@@ -2,7 +2,7 @@
|
||||
Response headers and bodies can optionally be rendered using [Handlebars.Net](https://github.com/rexm/Handlebars.Net) 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. To use this functionality, add `.WithTransformer()` to the response builder.
|
||||
|
||||
Example:
|
||||
C# Example:
|
||||
```csharp
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
@@ -11,13 +11,36 @@ server
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
.WithStatusCode(200)
|
||||
.WithHeader("Content-Type", "text/plain")
|
||||
.WithBody("Hello world! Your path is {{request.path}}.")
|
||||
.WithTransformer()
|
||||
);
|
||||
```
|
||||
|
||||
Mapping Json Example:
|
||||
``` js
|
||||
{
|
||||
"Guid": "fd8ca21b-db82-48bc-ae5a-fc2153c2b0db",
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "/bodyasfile_transform123"
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"get"
|
||||
]
|
||||
},
|
||||
"Response": {
|
||||
"Headers": { "Content-Type": "application/xml" },
|
||||
"BodyAsFile": "c:\\temp-wiremock\\__admin\\mappings\\_{{request.query.MyUniqueNumber}}_\\MyXmlResponse.xml",
|
||||
"UseTransformer": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### The request model
|
||||
The model of the request is supplied to the header and body templates. The following request attributes are available:
|
||||
|
||||
@@ -40,6 +63,11 @@ The model of the request is supplied to the header and body templates. The follo
|
||||
All of the standard helpers (template functions) provided by the C# Handlebars implementation plus all of the string helpers are available e.g.
|
||||
`{{capitalize request.query.search}}`
|
||||
|
||||
|
||||
### Transform the content from a referenced file
|
||||
By default, only the response (headers, statuscode, body) are transformed when the `.WithTransformer()` or `UseTransformer` are defined.
|
||||
In case you also want to transform the contents from a referenced file (via `BodyAsFile`), an additional parameter need to added. Like `.WithTransformer(bool)` or `UseTransformerForBodyAsFile = true`.
|
||||
|
||||
### Generate Random Data Support
|
||||
It's possible to return random data using the `Random` Handlebars function.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user