我可以从 GET 方法获取查询参数并放入响应中吗?
Can I get query params from GET-method and put into response?
我可以从 GET 方法获取查询参数并放入响应中吗?
比如我有mock:
"request": {
"method": "GET",
"urlPathPattern": "/cashboxes/[0-9]+/registration"
},
"response": {
"status": 200,
"jsonBody": {}
}
我想要,输入数字 [0-9]+ 用查询参数的名称替换到正文中。
如我所见:
URL: http://baseUrl/cashboxes/1/registration
"response": {
"status": 200,
"jsonBody": {
"cashboxes_id": "1"
}
}
这当然可以通过Response Templates的机制实现。这允许您使用许多请求元数据项,还可以通过其对 Handlebars 模板的支持使用正文本身。
在你的例子中它会是这样的:
{
"request": {
"method": "GET",
"urlPathPattern": "/cashboxes/[0-9]+/registration/[0-9]+"
},
"response": {
"status": 200,
"jsonBody": {
"status": "status one",
"URLpat1": "{{request.path.[1]}}",
"URLpat2": "{{request.path.[3]}}"
},
"headers": {
"Content-Type": "application/json"
},
"transformers": ["response-template"]
}
}
并且发送此 GET 请求时:
http://localhost:9999/cashboxes/1000/registration/2000
然后收到此响应:
{
"status": "status one",
"URLpat1": "1000",
"URLpat2": "2000"
}
我可以从 GET 方法获取查询参数并放入响应中吗?
比如我有mock:
"request": {
"method": "GET",
"urlPathPattern": "/cashboxes/[0-9]+/registration"
},
"response": {
"status": 200,
"jsonBody": {}
}
我想要,输入数字 [0-9]+ 用查询参数的名称替换到正文中。
如我所见:
URL: http://baseUrl/cashboxes/1/registration
"response": {
"status": 200,
"jsonBody": {
"cashboxes_id": "1"
}
}
这当然可以通过Response Templates的机制实现。这允许您使用许多请求元数据项,还可以通过其对 Handlebars 模板的支持使用正文本身。
在你的例子中它会是这样的:
{
"request": {
"method": "GET",
"urlPathPattern": "/cashboxes/[0-9]+/registration/[0-9]+"
},
"response": {
"status": 200,
"jsonBody": {
"status": "status one",
"URLpat1": "{{request.path.[1]}}",
"URLpat2": "{{request.path.[3]}}"
},
"headers": {
"Content-Type": "application/json"
},
"transformers": ["response-template"]
}
}
并且发送此 GET 请求时:
http://localhost:9999/cashboxes/1000/registration/2000
然后收到此响应:
{
"status": "status one",
"URLpat1": "1000",
"URLpat2": "2000"
}