request.query.<key>.[<n>] is not working & request.query.<key>.[0] produces all query parameter at once

121 Views Asked by At

I am using wiremock-jre8-standalone-2.35.0.jar

This is my mapping file -

{
    "request": {
        "urlPattern": "/fulfillmentstate\\?fulfillmentid=SVT-ETA-\\d+(%2CSVT-ETA-\\d+)*",
        "method": "GET",
        "queryParameters" : {
            "fulfillmentid" : {
                "matches" : "^SVT-ETA-\\d+(,SVT-ETA-\\d+)*$"
            }
        }
        
    },
    "response": {
        "status": 200,
        "fixedDelayMilliseconds": 500,
         "transformers": [
            "response-template"
        ],
        "headers": {
            "Content-Type": "application/json"
        },
        "bodyFileName": "fulfillmentstate_qpm.json"
    }
}

The URL I am hitting via postman -

http://localhost:8080/fulfillmentstate?fulfillmentid=SVT-ETA-1234%2CSVT-ETA-234%2CSVT-ETA-344

Now Response Templating that I have used in fulfillmentstate_qpm.json is -

{{request.query.fulfillmentid.[0]}}

Expected : SVT-ETA-1234 Actual : SVT-ETA-1234,SVT-ETA-234,SVT-ETA-344

This is the official documentation : https://wiremock.org/docs/response-templating/ (Under heading The Request Model)

I need to use these query parameter in a response file where I need to add multiple json attribute for each query parameter.

I have tried {{request.query.fulfillmentid.[0]}} {{request.requestLine.query.fulfillmentid.[0]}}

1

There are 1 best solutions below

0
Priyesh Yadav On

This is an Alternative : if the Request URL is :

http://localhost:8080/fulfillmentstate?fulfillmentid=SVT-ETA-1234%2CSVT-ETA-234%2CSVT-ETA-344

here the URL have query parameter separated by comma, to access each element by index is :

{{ regexExtract request.query.fulfillmentid '([^,]+)' 'values'}}{{values.0}}
{{ regexExtract request.query.fulfillmentid '([^,]+)' 'values'}}{{values.1}}
{{ regexExtract request.query.fulfillmentid '([^,]+)' 'values'}}{{values.2}}

Above expression will respectively produce

SVT-ETA-1234
SVT-ETA-234
SVT-ETA-344