-
-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Support for JsonPath in the response (with HandleBars) #167
Comments
Currently it's only supported that you can use a jsonmatcher on the request. See https://github.com/WireMock-Net/WireMock.Net/wiki/Stubbing-and-Request-Matching#json-jsonmatcher It's not yet supported that you can use JsonPath in the output, like you can do with handlebars : https://github.com/WireMock-Net/WireMock.Net/wiki/Stubbing-and-Request-Matching#response-templating |
Is there a plan to do so? Is there a HandleBar feature I can use instead to extract parts from the request body? |
There is no HandleBars functionality out of the box. I can extend the code to support something like this: // select single token
"{{JSONPath.SelectToken response.body \"$.Manufacturers[?(@.Name == 'Acme Co')]\"}}" Where response.body is the response body in json. Also selecting multiple tokens should be possible: // select multiple tokens
"{{#JSONPath.SelectTokens response.body \"$..Products[?(@.Price >= 50)].Name\"}}{{token}}{{/JSONPath.SelectTokens}}" |
That sounds great. When do you think you can add this feature? |
Currently I am fixing this issue #148 (comment). After that I will continue on this one. If the proposed fix here is enough for you and will comply to all your requirements, I will implement it as is. |
Preview NuGet can be found at Note that the syntax has been changed from |
Thanks StefH. I will take a look and get back to you. |
My request input I'm trying to match is
using
but I get
What am I doing wrong? |
1] You want to use the request I guess So: See also this code : https://github.com/WireMock-Net/WireMock.Net/blob/SupportJSONPath_in_the_response/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithHandlebarsTests.cs#L384 |
Yep should have used request.body but still getting
The input request I'm trying to grab data from is very simple just:
I'm just trying to make "Zubin" appear the response. |
You should use username instead of @.username. {
"Guid": "407e031f-c96b-429d-8a58-01c6a456fedd",
"Priority": 0,
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/zubinix",
"IgnoreCase": false
}
]
},
"Methods": [
"post"
]
},
"Response": {
"StatusCode": 200,
"BodyDestination": "SameAsSource",
"Body": "{ \"result\": \"{{JsonPath.SelectToken request.body \"username\"}}\" }",
"UseTransformer": true,
"Headers": {
"Content-Type": "application/json"
}
}
} Note that you also can use request.bodyAsJson. |
That did the trick. Thanks. |
I did update the code, so now also BodyAsJson (in the response) is supported, see this mapping: {
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/zubinix2",
"IgnoreCase": false
}
]
},
"Methods": [
"post"
],
"Body": {}
},
"Response": {
"StatusCode": 200,
"BodyAsJson": {
"path": "{{request.path}}",
"result": "{{JsonPath.SelectToken request.bodyAsJson \"username\"}}"
},
"UseTransformer": true,
"Headers": {
"Content-Type": "application/json"
}
}
} New Nuget (1.0.4.8-preview-01) is available. Please test |
Trying the above. What could be causing this error?
For input "Response": {
"UseTransformer": true,
"StatusCode": 200,
"BodyAsJson": {
"path": "{{request.path}}",
"result": "{{JsonPath.SelectToken request.bodyAsJson \"username\"}}"
},
"Headers": {
"Content-Type": "application/json"
}
} |
Oh yes, I was missing the header in the request. Works now but interesting if I delete the header it still works! So the header is required the first time only? I'm using fiddler and postman as follows:
|
Very strange. There is no caching on the request, that would break the whole WireMock functionality. For now, do you think it works as excepted ? If so, this issue can be closed, and then I'll create a new official NuGet. |
Yes. Please close and create the NuGet. Thanks! I might create another issue relating to this apparent caching after get some more experience this tool. |
Support for Json request parameters in response has been added. |
Does WireMock.Net have support for the jsonPath helper found in the java based WireMock? I want to create a template that inserts in the response items from a JSON request. It seems all I can do with WireMock-Net 'out of the box' is use {{request.body}} in the response template which returns the entire JSON body of the request?
The text was updated successfully, but these errors were encountered: