动态 return 基于查询参数的文件
Dynamically return a file based on query argument
我在我的映射目录中定义了这个存根。我正在尝试 return 根据我的查询参数的值动态生成一个文件(查询参数和文件名将具有相同的名称)
请看下面的例子。
{
"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 表示查询参数的定位,零索引。
找出不同响应模板变量代表的值并不困难。下面是 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"
]
}
}
我在我的映射目录中定义了这个存根。我正在尝试 return 根据我的查询参数的值动态生成一个文件(查询参数和文件名将具有相同的名称)
请看下面的例子。
{
"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 表示查询参数的定位,零索引。
找出不同响应模板变量代表的值并不困难。下面是 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"
]
}
}