Dynamic response files using Handlebars templating #173

Closed
opened 2025-12-29 14:24:23 +01:00 by adam · 4 comments
Owner

Originally created by @denstorti on GitHub (May 3, 2019).

Hi,

I have a question about the current capabilities of templating.

I'd like to retrieve responses from files using WireMock.Net, and for that I am using static mappings with "BodyAsFile" attribute.

Example:

{
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/bodyasfilexmltest",
                    "IgnoreCase": false
                }
            ]
        },
        "Methods": [
            "get"
        ]
    },
  "Response": {
    "StatusCode": 200,
    "Headers": { "Content-Type": "application/xml" },
    "BodyAsFile": "c:\\temp-wiremock\\__admin\\mappings\\MyXmlResponse.xml",
    "UseTransformer": false
  }
}

But I'd like to vary the response directory based on the request attributes, pretty much as Handlebars templates do.

Example (MyUniqueNumber would be the name of each folder):

"BodyAsFile": "c:\\temp-wiremock\\__admin\\mappings\\_{{request.query.MyUniqueNumber}}_\\MyXmlResponse.xml",

In this example the variable is in the Path, but it could be appended to the file name as well.

I did test it and it seems the library tries to load the file from disk when loaded and not per request, so it cannot find the invalid path "c:\temp-wiremock\__admin\mappings\{{request.query.MyUniqueNumber}}\MyXmlResponse.xml".

Is this scenario currently supported (maybe in a different way)?

Originally created by @denstorti on GitHub (May 3, 2019). Hi, I have a question about the current capabilities of templating. I'd like to retrieve responses from files using WireMock.Net, and for that I am using static mappings with "BodyAsFile" attribute. Example: ``` js { "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/bodyasfilexmltest", "IgnoreCase": false } ] }, "Methods": [ "get" ] }, "Response": { "StatusCode": 200, "Headers": { "Content-Type": "application/xml" }, "BodyAsFile": "c:\\temp-wiremock\\__admin\\mappings\\MyXmlResponse.xml", "UseTransformer": false } } ``` But I'd like to vary the response directory based on the request attributes, pretty much as Handlebars templates do. Example (MyUniqueNumber would be the name of each folder): "BodyAsFile": "c:\\temp-wiremock\\__admin\\mappings\\_{{request.query.MyUniqueNumber}}_\\MyXmlResponse.xml", In this example the variable is in the Path, but it could be appended to the file name as well. I did test it and it seems the library tries to load the file from disk when loaded and not per request, so it cannot find the invalid path "c:\\temp-wiremock\\__admin\\mappings\\_{{request.query.MyUniqueNumber}}_\\MyXmlResponse.xml". Is this scenario currently supported (maybe in a different way)?
adam added the questionbug labels 2025-12-29 14:24:23 +01:00
adam closed this issue 2025-12-29 14:24:23 +01:00
Author
Owner

@StefH commented on GitHub (May 3, 2019):

This scenario is not yet supported, I'll take a look and modify the code.

@StefH commented on GitHub (May 3, 2019): This scenario is not yet supported, I'll take a look and modify the code.
Author
Owner

@StefH commented on GitHub (May 3, 2019):

@denstorti
Can you try MyGet version WireMock.Net.1.0.15-ci-11270 ?

Note that you need to set "UseTransformer": true in the mapping.

@StefH commented on GitHub (May 3, 2019): @denstorti Can you try MyGet version WireMock.Net.1.0.15-ci-11270 ? Note that you need to set `"UseTransformer": true` in the mapping.
Author
Owner

@denstorti commented on GitHub (May 4, 2019):

@StefH, I could test it using the MyUniqueNumber as query string and also as JSON Path

Example using JSON Path

Input data (save as data.json):

{
  "MyUniqueNumber": "4444"
}

Mapping:

{
  "Request": {
    "Path": {
      "Matchers": [
        {
          "Name": "WildcardMatcher",
          "Pattern": "/postbodyasfile",
          "IgnoreCase": false
        }
      ]
    },
    "Methods": [
      "post"
    ]
  },
  "Response": {
    "StatusCode": 200,
    "Headers": { "Content-Type": "application/json" },
    "BodyAsFile": "c:/MockResponses/{{JsonPath.SelectToken request.body \"$.MyUniqueNumber\"}}/serv1.json",
    "UseTransformer": true
  }
}

curl test:
curl -d @data.json http://localhost:9091/postbodyasfile

Thank you!

@denstorti commented on GitHub (May 4, 2019): @StefH, I could test it using the MyUniqueNumber as query string and also as JSON Path **Example using JSON Path** Input data (save as data.json): ``` { "MyUniqueNumber": "4444" } ``` Mapping: ``` { "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/postbodyasfile", "IgnoreCase": false } ] }, "Methods": [ "post" ] }, "Response": { "StatusCode": 200, "Headers": { "Content-Type": "application/json" }, "BodyAsFile": "c:/MockResponses/{{JsonPath.SelectToken request.body \"$.MyUniqueNumber\"}}/serv1.json", "UseTransformer": true } } ``` curl test: ``` curl -d @data.json http://localhost:9091/postbodyasfile ``` Thank you!
Author
Owner

@StefH commented on GitHub (May 4, 2019):

OK.

I will release a new NuGet today.

Note that instead of JsonPath, you can also use JmesPath as a matcher.
http://jmespath.org/
https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching#jmes-path-jmespathmatcher

@StefH commented on GitHub (May 4, 2019): OK. I will release a new NuGet today. Note that instead of JsonPath, you can also use JmesPath as a matcher. http://jmespath.org/ https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching#jmes-path-jmespathmatcher
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#173