mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
Response Templating_Method not found error #331
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
Response:
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:
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?
@StefH commented on GitHub (Feb 10, 2021):
@jthomas2021
You could use something like this:
And
"Pattern": "$..[?(@.Accountnumber != 7654322)]"--> return body with "NotFound/Error/..."@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.
@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:
For the response you need to use this: https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating
@StefH commented on GitHub (Feb 15, 2021):
Hello @jthomas2021, is the answer clear to you?
@jthomas2021 commented on GitHub (Feb 15, 2021):
Thanks @StefH it worked! Thank you for your help!