mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Feature: Support for JsonPath in the response (with HandleBars) #118
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 @zubinix on GitHub (Jul 18, 2018).
Originally assigned to: @StefH on GitHub.
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?
@StefH commented on GitHub (Jul 18, 2018):
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
@zubinix commented on GitHub (Jul 18, 2018):
Is there a plan to do so? Is there a HandleBar feature I can use instead to extract parts from the request body?
@StefH commented on GitHub (Jul 18, 2018):
There is no HandleBars functionality out of the box. I can extend the code to support something like this:
Where response.body is the response body in json.
And the $.Manufacturers[?(@.Name == 'Acme Co')] is the JSONPath expression (copied from example here: https://www.newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm)
Also selecting multiple tokens should be possible:
@zubinix commented on GitHub (Jul 19, 2018):
That sounds great. When do you think you can add this feature?
@StefH commented on GitHub (Jul 19, 2018):
Currently I am fixing this issue https://github.com/WireMock-Net/WireMock.Net/issues/148#issuecomment-406163503.
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.
@StefH commented on GitHub (Jul 19, 2018):
Preview NuGet can be found at
https://www.nuget.org/packages/WireMock.Net/1.0.4.7-preview-jsonpath
Note that the syntax has been changed from
JSONPath.SelectTokentoJsonPath.SelectToken!@zubinix commented on GitHub (Jul 20, 2018):
Thanks StefH. I will take a look and get back to you.
@zubinix commented on GitHub (Jul 20, 2018):
My request input I'm trying to match is
{"username": "Zubin"}using
but I get
What am I doing wrong?
@StefH commented on GitHub (Jul 20, 2018):
1] You want to use the request I guess
2] Also try request.bodyAsJson instead of request.body?
So:
{{JsonPath.SelectToken request.bodyAsJson \"@.username\"}}See also this code : https://github.com/WireMock-Net/WireMock.Net/blob/SupportJSONPath_in_the_response/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithHandlebarsTests.cs#L384
@zubinix commented on GitHub (Jul 20, 2018):
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.
@StefH commented on GitHub (Jul 20, 2018):
You should use username instead of @.username.
And the BodyAsJson does not (yet) work, so you need to use this solution:
Note that you also can use request.bodyAsJson.
@zubinix commented on GitHub (Jul 20, 2018):
That did the trick. Thanks.
@StefH commented on GitHub (Jul 20, 2018):
I did update the code, so now also BodyAsJson (in the response) is supported, see this mapping:
New Nuget (1.0.4.8-preview-01) is available.
Please test
@zubinix commented on GitHub (Jul 23, 2018):
Trying the above. What could be causing this error?
For input
@StefH commented on GitHub (Jul 23, 2018):
When you send a request, do you also provide the
Content-Typeheader with valueapplication/json?Like:

@zubinix commented on GitHub (Jul 23, 2018):
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:
@StefH commented on GitHub (Jul 23, 2018):
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.
@zubinix commented on GitHub (Jul 23, 2018):
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.
@zubinix commented on GitHub (Jul 23, 2018):
Support for Json request parameters in response has been added.