我在我的映射目录中定义了这个存根。我正在尝试根据查询参数的值动态返回文件(查询参数和文件名将具有相同的名称)
请参见下面的示例。
{
"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"
]
}
}
GET请求类似于
.../some/url/locales/en_GB/products?typeName=blah
在我的__files目录中,我有一个名为blah.json的文件,我希望响应匹配。
我不确定bodyFileName
是否正确。 5表示零索引的查询参数的位置。
答案 0 :(得分:1)
弄清楚不同的响应模板变量代表什么值并不困难。以下是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"
]
}
}
并且GET请求http://localhost:8080/some/url/locales/en_GB/products?typeName=test
导致以下响应:
request.requestline.pathSegments:
[0]: some
[1]: url
[2]: locales
[3]: en_GB
[4]: products
[5]:
request.query.typeName: test
考虑到以上几点,您可以看到所寻找的值不是使用request.pathSegments[x]
而是使用request.query.typeName
检索的。结果如下:
{
"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"
]
}
}