Getting request URL path in Response Body

497 views
Skip to first unread message

Arun Dharmar

unread,
Feb 6, 2018, 9:55:46 AM2/6/18
to wiremock-user

I have a scenario to mock. (Using only JSON)
Here is the request URL
https://somhost.com/sometext1/sometext2/12345
Here is my mapping file

{
  "uuid" : "f2881f58-9499-372f-be2b-d3602d75f7b2",
  "priority": 1,
  "request" : {
    "urlPattern" : "/sometext1/sometext2/(.*)",
    "method" : "GET",
    "headers": {
      "X-Correlation-Id": {
        "equalTo": "proxy_1"
      }
    }
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "ResponseFile.json",
    "transformers": ["response-template"]
  }
}

Here is the ResponseFile.json

{
  "OrderId":"$$request.path.[2]$$",
  "status": "Pending",
  "etag": "2f599661db977106441f641e69920378"
}

I want 12345 from my request url into the response body for the JSON tag OrderId. I tried a few combination like the one I gave above $$request.path.[2]$$. But none of them are working. Any help in this regard is much appreciated.

Tom Akehurst

unread,
Feb 6, 2018, 9:59:37 AM2/6/18
to wiremock-user
You need to use {{ and }} as template tag delimiters, rather than $$

Arun Dharmar

unread,
Feb 6, 2018, 3:02:12 PM2/6/18
to wiremock-user
I tried that too. It is not working. Is there a sample project that has the templating in body file that works.

Tom Akehurst

unread,
Feb 7, 2018, 5:09:55 AM2/7/18
to wiremock-user
Here's a test class showing templating in action: https://github.com/tomakehurst/wiremock/blob/master/src/test/java/com/github/tomakehurst/wiremock/ResponseTemplatingAcceptanceTest.java#L34

How are you starting WireMock? Are you running standalone, and if so what command line are you entering?

Arun Dharmar

unread,
Feb 7, 2018, 9:18:03 AM2/7/18
to wiremock-user
Tom,

I am using wiremock deployed in a cloud server.

The command to start wiremock is

java -jar /app/wiremock-standalone-*.jar --port 8080 --verbose

Is that what you were looking for?

Thanks,
Arun.

Tom Akehurst

unread,
Feb 7, 2018, 12:22:44 PM2/7/18
to wiremock-user
You need to enable response templating. See the CLI arguments in http://wiremock.org/docs/running-standalone/

Arun Dharmar

unread,
Feb 7, 2018, 2:15:57 PM2/7/18
to wiremock-user
Awesome! That worked. Thanks a lot
Reply all
Reply to author
Forward
0 new messages