Wiremock not templating request path segments

2.6k Views Asked by At

I am trying to use the request.pathSegments to render some information in a response and that placeholder is empty...

using 2.26

Wiremock standalone start up cmd:

java -jar ./wiremock.jar --root-dir "/usr/share/wiremock" --verbose --local-response-templating

Stub definition:

{
  "mappings": [
    {
      "request": {
        "method": "GET",
        "urlPathPattern": "/v2/path/data/.*"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "bodyFileName": "responses/data.json",
        "transformers": ["response-template"]
      }
    }
  ]
}

template located in __files/responses/data.json:

{
  "data": [
    {
      "id": "{{request.pathSegments.[3]}}",
      "type": "values"
    },
    {
      "id": "{{request.pathSegments.[3]}}",
      "type": "values"
    }
  ]
}

call to http://localhost:8000/v2/path/data/foo

expected:

{
  "data": [
    {
      "id": "foo",
      "type": "values"
    },
    {
      "id": "foo",
      "type": "values"
    }
  ]
}

actual:

{
  "data": [
    {
      "id": "",
      "type": "values"
    },
    {
      "id": "",
      "type": "values"
    }
  ]
}

I have tried using {{request.path}} in teh template and that IS rendered correctly.

If anyone can spot what I'm missing...

1

There are 1 best solutions below

0
On BEST ANSWER

Try using triple handlebars and path instead of pathSegments.


  "data": [
    {
      "id": "{{{request.path.[3]}}}",
      "type": "values"
    },
    {
      "id": "{{{request.path.[3]}}}",
      "type": "values"
    }
  ]
}

I find the documentation on this somewhat vague, and I usually default to triple, and then try double if the triple doesn't work. I also avoid using pathSegments when possible.