First check: did you enable response templating wh...
# help
b
First check: did you enable response templating when starting the standalone server using either —global-response-templating or —local-response-templating? See https://wiremock.org/docs/running-standalone/#command-line-options
k
I am using —global-response-templating please have a look on my command
Copy code
java -cp "wiremock-webhooks-extension-2.32.0.jar;wiremock-standalone-2.32.0.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --extensions "org.wiremock.webhooks.Webhooks" --local-response-templating --global-response-templating --port 7879 --verbose
b
Have you tried starting your WireMock server that way and creating a simple test that uses response templating in a regular response definition? Also, I’m not sure response templating works with post-serve actions, maybe @Oleg Nenashev can give you a more conclusive answer?
k
Thanks @Bas Dijkstra for the response, I have tried many other scenarios as well and the response template is working but the only issue is with post-serve actions.
b
You’re welcome. I don’t know the WireMock source code well enough but I suspect that by the time the post-serve action is triggered, the response has been sent and the request has been discarded (except in the log). But I may be completely wrong :) I’m sure someone from the core WireMock team can shed more light on this.
🙌 1
o
will catch up in 1 hr or so
1
n
@Kaushal Gupta have you tried to use
Copy code
{{jsonPath originalRequest.body '$.baseUrl'}}
instead of {{request.baseUrl}} It looks like for templating in regular response supposed to be used keyword ‘*request*’ But for templating in postServeActions supposed to be used ‘*originalRequest*’
One of my playground examples
Copy code
var wiremockClient = new WireMock(wiremock.getServerPort());
wiremockClient.register(post(urlPathEqualTo("/" + CALLBACK_TRIGGER_PATH))
    .willReturn(ok()
        .withBody("Please Wait {{jsonPath request.body '$.callbackUrl'}}"))
    .withPostServeAction("webhook", webhook()
        .withMethod("{{jsonPath originalRequest.body '$.callbackMethod'}}")
        .withUrl("{{jsonPath originalRequest.body '$.callbackUrl'}}")
        .withBody("Async processing Finished")
        .withFixedDelay(1000))
);

// when
HttpResponse<String> response = TestClient.newInstance()
    .post(
        wiremock.getRequestURI(CALLBACK_TRIGGER_PATH),
        "{\"callbackMethod\": \"PUT\", \"callbackUrl\": \"" + callbackEndpointForWiremockStub  +"\"}"
    );
k
@Nikita Karpuk I tried after the replacement {{jsonPath originalRequest.body '$.baseUrl'}} from {{request.baseUrl}} but no luck.
Copy code
{{jsonPath originalRequest.body '$.baseUrl'}}
the value for above is coming as blank only. please refer to the below screenshot
n
Could you provide example of request you sending to wiremock And example of callback you are expecting
k
request example
Copy code
{
  "request": {
    "url": "/wiremock/middesk/v1/businesses",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesJsonPath": {
          "expression": "$..tin",
          "contains": "123"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "bodyFileName": "middesk-compliance-partner/customer_response/middeskInitiateResponse_success.json",
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    }
  },
  "postServeActions": [
    {
      "name": "webhook",
      "parameters": {
        "headers": {
          "Content-Type": "application/json"
        },
        "method": "POST",
        "url": "{{jsonPath originalRequest.body '$.baseUrl'}}/callback",
        "delay": {
          "type": "fixed",
          "milliseconds": 10
        }
      }
    }
  ]
}
n
I mean real request that application sends Like
Copy code
POST 127.0.0.1:8888/wiremock/middesk/v1/businesses
{
 "baseUrl": "my-callback-url"
 "someOtherField" : "some value"
}
I am trying to understand if
baseUrl
a part of body or something else
k
yes curl --location 'http://127.0.0.1:7879/wiremock/middesk/v1/businesses' \ --header 'Content-Type: application/json' \ --data '{ "tin":"123" }'
baseUrl
is not a part of body
n
Could you also provide example of expected callback url?
k
so basically host and port should be remain same as incoming request,,
n
you could try to play with params like
originalRequest.url
- URL path and query - example
{{originalRequest.url}}
originalRequest.path
- URL path - example
{{originalRequest.path}}
originalRequest.pathSegments.[<n>]
- URL path segment (zero indexed) e.g.
{{originalRequest.pathSegments.[2]}}
And
originalRequest.baseUrl
- URL up to the start of the path e.g. https://my.example.com:8080
{{originalRequest.baseUrl}}
k
Thanks @Nikita Karpuk it worked finally for me, very very thanks u so much :)
👍 1
n
@Kaushal Gupta you are very welcome
❤️ 1
k
@Nikita Karpuk @Oleg Nenashev Good Morning, I need your help as I am trying to perform one more scenario that is I am trying to send the one field value in callback Url as a query parameter from the stubbed response and also using the same host and port of incoming coming request for callback URL: post server action callback URL will be like below:
Copy code
"url": "<incomingRequestHost>:<incomingRequestPort>/middesk/callback?id={{response.id}}

where id is the field inside the stubbed response 
 "id": "{{randomValue type='UUID'}}",
but above one example is not working
n
@Kaushal Gupta as far as i know you could not get value from response to use it in a postServeAction Personally i did it via Dynamic Stabs using Java 1.
var id = UUID._randomUUID_().toString()
2. Create dynamic stub with response and postServeAction
Copy code
wiremockClient.register(post(urlPathEqualTo(CALLBACK_TRIGGER_PATH))
            .willReturn(ok()
                ...
                .withBody("id =" + id))
            .withPostServeAction("webhook", webhook()
                ...
                .withUrl("..." + "?id=" + id)
                .withFixedDelay(1000))
        );
k
Hi @Nikita Karpuk I got the work around to resolve this : 1. used ninecookies callback simulator com.ninecookies.wiremock.extensions.CallbackSimulator 2. created one more fields in stubbed response like "baseUrl": "{{request.baseUrl}}". stubbed response body is below:
Copy code
{
  "id": "{{randomValue type='UUID'}}",
  "name": "{{jsonPath request.body '$.name'}}",
  "baseUrl": "{{request.baseUrl}}",
  "tags": [],
  "addresses": [
    {
      "object": "address",
      "address_line1": "{{jsonPath request.body '$.addresses[0].address_line1'}}",
      "address_line2": "{{jsonPath request.body '$.addresses[0].address_line2'}}",
      "city": "{{jsonPath request.body '$.addresses.[0].city'}}",
      "state": "{{jsonPath request.body '$.addresses.[0].state'}}",
      "postal_code": "{{jsonPath request.body '$.addresses[0].postal_code'}}",
      "created_at": "2019-02-04T18:17:20.533Z",
      "updated_at": "2019-02-04T18:17:20.533Z"
    }
  ]
}
3. Created post server action like below
Copy code
"postServeActions": {
    "callback-simulator": {
      "callbacks": [
        {
          "delay": 10,
          "url": "$(response.baseUrl)/middesk/callback",
          "data": {
            "object": "event",
            "id": "$(response.id)",
            "type": "business.updated",
            "data": {
              "object": {
                "id": "$(response.id)",
                "created_at": "2023-05-02T23:54:48.154Z",
                "updated_at": "2023-05-02T23:54:48.154Z",
                "external_id": null,
                "phone_numbers": [],
                "registrations": []
              }
            },
            "created_at": "2023-05-02T23:54:48.239Z"
          }
        }
      ]
    }
  }
4. run the standalone server with below command.
Copy code
java -cp "wiremock-webhooks-extension-2.32.0.jar;wiremock-standalone-2.32.0.jar;wiremock-extensions-0.0.6-jar-with-dependencies.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --extensions "com.ninecookies.wiremock.extensions.CallbackSimulator,com.ninecookies.wiremock.extensions.JsonBodyTransformer,org.wiremock.webhooks.Webhooks" --port 7879 --verbose --local-response-templating --global-response-templating
It worked successfully !!! :)
👍 1
o
👍
🙌 1