Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

345
Views
Wiremock Stand alone - How to manipulate response with request data

I was trying to implement mocking of POST REST call using Wiremock Standalone server. I am faced with a challenge like this, suppose the post body contains an "name" field and its value, the same value should be returned in the response of that POST call. My json file looks like this:

{
"priority": 1,
"request": {
    "method": "POST",
    "urlPath": "/primeSlots",
    "bodyPatterns" : [ {
        "matchesJsonPath" : "{ \"things\": [ { \"name\": \"794363\" }] 
 }"
    } ]
 },
 "response": {
    "status": 200,
    "body": "{{$.things.name.value}}",
    "transformers": ["response-template"]
 }
}

so, I need to get the value , that is 794363,but using above approach not able to get it in the post response body.

I tried this too:

{
 "request": {
    "method": "POST",
    "urlPath": "/transform",
    "bodyPatterns": [
        {
            "matchesJsonPath" : "$.things[?(@.name =~ /[0-9]+/i)]"
        }
    ]
  },
  "response": {
    "status": 200,
    "body": "{\"responseName\": \"
   {{request.body.things.name.value}}\"}",
    "headers": {
        "Content-Type": "application/json"
    },
    "transformers": ["body-transformer"]
  }
 }

So my question is, even if i use a regEx, which matches with any number that comes in the request, how to give back the same number in the response using Wiremock standalone json file? Thanks.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Unfortunately WireMock's response templating transformer doesn't currently break out the request body into a Map as would be necessary for what you're trying to do. The request body is just a single string.

The simplest way for you to enable this would probably be to write a Handlebars helper that implements JSONPath or some other mechanism for querying a JSON document, then registering this with the templating transformer when you initialise WireMock.

At some point I'll write handlebars helpers to do this kind of thing for XML and JSON, but that won't be for a while.

about 4 years ago · Santiago Trujillo Report

0

Today I was in the same situation like you and found a solution which I would like to share with you:

  1. Create own class that extends ResponseDefinitionTransformer
  2. Add Handlebar functionality in your own transformer class ( see https://github.com/jknack/handlebars.java how to do it )
  3. (Optional) Append your own/other helpers e.g. Arrays.stream(StringHelpers.values()).forEach(helper -> this.handlebars.registerHelper(helper.name(), helper));
  4. Export your transformer customization as a separate JAR file
  5. Create a start batch script to start up your WireMock standalone with your own transformer extension e.g. java -cp "-cp ".\lib\wiremock-standalone-2.5.1.jar;.\lib\customTransformer.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --extensions "your.name.CustomTransformer" (for linux use : instead of ; as classpath separator)

In case you have multiple transformers, just define all those using , (comma) as separator in the --extensions arg.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!