I have this stub defined within my mappings directory. I'm trying to return a file dynamically based upon the value of my query parameter (the query param and file name will have the same name)
Please see below an example.
{
"request": {
"method": "GET",
"urlPathPattern": "/some/url/locales/en_GB/products",
"queryParameters": {
"typeName": {
"matches": "([A-Za-z]*)"
}
}
},
"response": {
"status": 200,
"bodyFileName" : "{{request.pathSegments.[5]}}",
"transformers": [
"response-template"
]
}
}
The GET request would look something like
.../some/url/locales/en_GB/products?typeName=blah
In my __files directory, I would have a file called blah.json which I want the response to match.
I'm not sure if the bodyFileName is correct. The 5 represents positioning of the query param, zero-indexed.
Figuring out what values are represented by the different Response Template variables is not difficult. Below is an example to g
{
"request": {
"method": "GET",
"urlPathPattern": "/some/url/locales/en_GB/products",
"queryParameters": {
"typeName": {
"matches": "([A-Za-z]*)"
}
}
},
"response": {
"status": 200,
"body" : "request.requestline.pathSegments:\n[0]: {{request.requestLine.pathSegments.[0]}}\n[1]: {{request.requestLine.pathSegments.[1]}}\n[2]: {{request.requestLine.pathSegments.[2]}}\n[3]: {{request.requestLine.pathSegments.[3]}}\n[4]: {{request.requestLine.pathSegments.[4]}}\n[5]: {{request.requestLine.pathSegments.[5]}}\n\nrequest.query.typeName: {{request.query.typeName}}",
"transformers": [
"response-template"
]
}
}
And a GET request http://localhost:8080/some/url/locales/en_GB/products?typeName=test results in this response:
request.requestline.pathSegments:
[0]: some
[1]: url
[2]: locales
[3]: en_GB
[4]: products
[5]:
request.query.typeName: test
With the above in mind you can see that the value you seek is not retrieved using request.pathSegments[x] but rather request.query.typeName. This results then into:
{
"request": {
"method": "GET",
"urlPathPattern": "/some/url/locales/en_GB/products",
"queryParameters": {
"typeName": {
"matches": "([A-Za-z]*)"
}
}
},
"response": {
"status": 200,
"bodyFileName": "{{request.query.typeName}}.json",
"transformers": [
"response-template"
]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With