Response Templating_Method not found error #333

Closed
opened 2025-12-29 15:21:08 +01:00 by adam · 5 comments
Owner

Originally created by @jthomas2021 on GitHub (Feb 9, 2021).

I am trying to create a wiremock service where if a request comes in matching say an account number, it will respond with a response of say Success and if another account number, it will respond with a response of Bad request. Unfortunately there are json tag elements in the response that will change depending on the request, e.g in my below example RequestId needs to be the same as that of the request, for the downstream system to accept it,else it will be a duplicate transaction , if I hard code the value
Request:

{
"RequestId" : "a43d1856",
....
"Accountnumber": "7654322"
}

Response:

{
"RequestId" : "a43d1856",
"ReasonCode" : "Success"
}

I used JsonPathMatcher to match the account number for the request, but I am finding it difficult to figure out how to substitute the dynamic request id from the incoming request to the response.

I tried:

"Response": {
"StatusCode": 200,
"BodyAsJson": {
"RequestId": "{{JsonPath.SelectToken request.body \"$.RequestId\"}}",
"ReasonCode" : "Success"
},
"UseTransformer": true,
"headers": {
"Content-Type": "application/json"
}
}

When I send a request through soapui matching the request, I get response : Method not found

Can you help me with what I am doing wrong?

Originally created by @jthomas2021 on GitHub (Feb 9, 2021). I am trying to create a wiremock service where if a request comes in matching say an account number, it will respond with a response of say Success and if another account number, it will respond with a response of Bad request. Unfortunately there are json tag elements in the response that will change depending on the request, e.g in my below example RequestId needs to be the same as that of the request, for the downstream system to accept it,else it will be a duplicate transaction , if I hard code the value Request: ``` json { "RequestId" : "a43d1856", .... "Accountnumber": "7654322" } ``` Response: ``` json { "RequestId" : "a43d1856", "ReasonCode" : "Success" } ``` I used JsonPathMatcher to match the account number for the request, but I am finding it difficult to figure out how to substitute the dynamic request id from the incoming request to the response. I tried: ``` json "Response": { "StatusCode": 200, "BodyAsJson": { "RequestId": "{{JsonPath.SelectToken request.body \"$.RequestId\"}}", "ReasonCode" : "Success" }, "UseTransformer": true, "headers": { "Content-Type": "application/json" } } ``` When I send a request through soapui matching the request, I get response : Method not found Can you help me with what I am doing wrong?
adam added the question label 2025-12-29 15:21:08 +01:00
adam closed this issue 2025-12-29 15:21:08 +01:00
Author
Owner

@StefH commented on GitHub (Feb 10, 2021):

@jthomas2021
You could use something like this:

{
    "Request": {
        "Methods": [
            "post"
        ],
        "Body": {
            "Matcher": {
            "Name": "JsonPathMatcher",
            "Pattern": "$..[?(@.Accountnumber == 7654322)]"
          }
        }
    },
    "Response": {
    	"StatusCode": 200,
        "Body": "Success"
    }
}

And
"Pattern": "$..[?(@.Accountnumber != 7654322)]" --> return body with "NotFound/Error/..."

@StefH commented on GitHub (Feb 10, 2021): @jthomas2021 You could use something like this: ``` json { "Request": { "Methods": [ "post" ], "Body": { "Matcher": { "Name": "JsonPathMatcher", "Pattern": "$..[?(@.Accountnumber == 7654322)]" } } }, "Response": { "StatusCode": 200, "Body": "Success" } } ``` And `"Pattern": "$..[?(@.Accountnumber != 7654322)]"` --> return body with "NotFound/Error/..."
Author
Owner

@jthomas2021 commented on GitHub (Feb 10, 2021):

@StefH
I understood the Request matcher piece of it and it works , thank you. But my question was how do I substitute the value for RequestId in the response, based on what request comes in.

For example if this is the request, that comes in at a particular time

{
"RequestId" : "a43d1856",
....
"Accountnumber": "7654322"
}

then in my json stubbing, I want to get the RequestId from the request( "a43d1856") and use that as my request id in my response.

"Response": {
"StatusCode": 200,
"BodyAsJson": {
"RequestId": "{{JsonPath.SelectToken request.body "$.RequestId"}}",
"ReasonCode" : "Success"
},
"UseTransformer": true,
"headers": {
"Content-Type": "application/json"
}
}

I was hoping that JsonPath.SelectToken would get the request body, look at the request id and get the value . But it does not work. It is throwing the Method not found error.

Please let me know what I am doing wrong.

Thank you.

@jthomas2021 commented on GitHub (Feb 10, 2021): @StefH I understood the Request matcher piece of it and it works , thank you. But my question was how do I substitute the value for RequestId in the response, based on what request comes in. For example if this is the request, that comes in at a particular time { "RequestId" : "a43d1856", .... "Accountnumber": "7654322" } then in my json stubbing, I want to get the RequestId from the request( "a43d1856") and use that as my request id in my response. "Response": { "StatusCode": 200, "BodyAsJson": { "RequestId": "{{JsonPath.SelectToken request.body \"$.RequestId\"}}", "ReasonCode" : "Success" }, "UseTransformer": true, "headers": { "Content-Type": "application/json" } } I was hoping that JsonPath.SelectToken would get the request body, look at the request id and get the value . But it does not work. It is throwing the Method not found error. Please let me know what I am doing wrong. Thank you.
Author
Owner

@StefH commented on GitHub (Feb 11, 2021):

I see.
In that case:

Step 1

You need to match the request using the code I provided in my comment.
JsonPath is only used for Request Matching --> https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching

Step 2

You want to use the transformer to return the RequestId, in this case use this code:

"Response": {
  "StatusCode": 200,
  "BodyAsJson": {
    "RequestId": "{{request.BodyAsJson.RequestId}}",
    "ReasonCode" : "Success"
  },
  "UseTransformer": true,
  "Headers": {
    "Content-Type": "application/json"
   }
}

For the response you need to use this: https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating

@StefH commented on GitHub (Feb 11, 2021): I see. In that case: # Step 1 You need to match the request using the code I provided in my comment. JsonPath is only used for _Request Matching_ --> https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching # Step 2 You want to use the transformer to return the RequestId, in this case use this code: ``` json "Response": { "StatusCode": 200, "BodyAsJson": { "RequestId": "{{request.BodyAsJson.RequestId}}", "ReasonCode" : "Success" }, "UseTransformer": true, "Headers": { "Content-Type": "application/json" } } ``` For the response you need to use this: https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating
Author
Owner

@StefH commented on GitHub (Feb 15, 2021):

Hello @jthomas2021, is the answer clear to you?

@StefH commented on GitHub (Feb 15, 2021): Hello @jthomas2021, is the answer clear to you?
Author
Owner

@jthomas2021 commented on GitHub (Feb 15, 2021):

Thanks @StefH it worked! Thank you for your help!

@jthomas2021 commented on GitHub (Feb 15, 2021): Thanks @StefH it worked! Thank you for your help!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#333