0

I want to extract an attribute's value from JSON request body coming in a request in a running standalone wiremock server and use the value in the response. Is there any way to this dynamically.

For example, if below is the request body :

{
    "name": "Dummy-Name"
}

I should be able to extract the value of name attribute and send it in the response like below:

{"Request_Name": "Dummy-Name"}

1 Answers1

1

You can use the Request model and JsonPath helpers to achieve this.

...
    "response": {
        "body": "{ "Request_Name": {{jsonPath request.body '$.name'}} }"
    }
...

Note: you will need to enable response templating before this will work.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • Thanks! Also, does it work properly if I use the jsonPath helpers in my response headers as well to send some request body data? – Sumeet Ambastha Oct 01 '21 at 05:20
  • Also, is it possible to use attribute in the body of one request in another request's response body ? – Sumeet Ambastha Oct 01 '21 at 05:33
  • `jsonPath helpers in my response headers` -> Yes, it should work the same. `attribute in the body of one request in another request's response body` -> Yes, but you'd need to write a custom response transformer or introduce some of your own custom logic to maintain that state (the request body for A) and return it in the other request (B). – agoff Oct 01 '21 at 13:03
  • Is there any link I can refer for writing custom response transformers ? Also, if I need to introduce some custom logic, where do I need to write that exactly ? – Sumeet Ambastha Oct 01 '21 at 13:07
  • http://wiremock.org/docs/extending-wiremock/#response-definition-transformation – agoff Oct 01 '21 at 13:08