How to send a mock webhook with body from Wiremock response body

117 Views Asked by At

I try to create a mock webhook request, which will be triggered when request /transform is call in webhook request body will get the id from response body id. How can I achieve that?

{
  "request": {
    "method": "POST",
    "urlPath": "/transform"
  },
  "response": {
    "status": 200,
    "body": "{\"message\": \"success\", \"id\": \"{{randomValue length=10 type='ALPHANUMERIC'}}\"}",
    "headers": {
      "Content-Type": "application/json"
    }
  },
  "serveEventListeners": [
    {
      "name": "webhook",
      "parameters": {
        "method": "POST",
        "url": "https://webhook.site/9c32fadb-b542-4edf-a530-85a012064566",
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "{\"id\": \"{{jsonPath response.body '$.id'}}\"}"
      }
    }
  ]
}

I'd try with body-transformer, but still not able to get the id value from response

"transformers":[  
            "body-transformer"
         ]
1

There are 1 best solutions below

0
Lee On

This conversation was had on the Wiremock community slack here - https://wiremock-community.slack.com/archives/C03NAEH5LVA/p1705654286090779

The answers are replicated here for completeness:

The response object is not available in the templating system for webhooks. From looking at the documentation it seems you can only use the original request in the templating of the webhook payload:

{
  "request": {
    "method": "POST",
    "urlPath": "/webhooks"
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "message": "success",
      "transactionId": "{{jsonPath request.body '$.id'}}"
    },
    "headers": {
      "Content-Type": "application/json"
    },
    "transformers": ["response-template"]
  },
  "serveEventListeners": [
    {
      "name": "webhook",
      "parameters": {
        "method": "POST",
        "url": "http://host.docker.internal:7070/callback-logger/api/callback",
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "{\"systemId\": \"{{jsonPath originalRequest.body '$.id'}}\"}"
      }
    }
  ]
}

Request body:

{
  "id": "1234567890"
}

At the moment the only way to simulate what you are looking for is to send in the value as a value in the request body (as above) or as a request header in your test. That way the value will be available in the response and webhook.

This isn't ideal and we do have an issue raised to address this - https://github.com/wiremock/wiremock/issues/2359