wiremock - 如何使用 Wiremock 在字段中返回请求正文作为响应
问题描述
我有一个 JSON 请求如下:
{
"fieldOne": 12345,
"fieldTwo": 1234,
"fieldThree": "2019-12-05T12:32:42.323905",
"fieldFour": "string",
"fieldFive": 5432,
"fieldSix": "string",
"fieldSeven": "string",
"fieldEight": "string"
}
我需要在字段中发送完整的请求 JSON 对象作为响应。我的 Wiremock 存根 JSON 是,
{
"request": {
"method": "POST",
"urlPath": "/endpoint"
},
"response": {
"status": 200,
"jsonBody": {
"request": "{{{request.body}}}", //If I remove quotes here then I get error so I added the quotes
"anotherField": "string"
},
"headers": {
"Content-Type": "application/json;charset=UTF-8"
},
"transformers": ["response-template"]
}
}
如何在字段中发送请求正文作为响应?
我现在得到错误:
Illegal unquoted character ((CTRL-CHAR, code 10)):
has to be escaped using backslash to be included in string value\n at [Source: (
编辑:-
2.19.0
我正在使用导致此问题的wiremock版本。我将版本升级到2.21.0
现在问题已解决
但是在响应中,我仍然遇到以下问题,请求正文在双引号内,这是一个无效的 JSON。回复:-
{
"request": "{ //Here the double quotes should not be present before curly brace
"fieldOne": 12345,
"fieldTwo": 1234,
"fieldThree": "2019-12-05T12:32:42.323905",
"fieldFour": "string",
"fieldFive": 5432,
"fieldSix": "string",
"fieldSeven": "string",
"fieldEight": "string"
}",
"anotherField": "string"
}
解决方案
使用body
而不是jsonBody
. 然后响应消息(正文中双引号内的内容)可以以任何需要的方式格式化。
这也适用于wiremock 2.19.0 版本
{
"request": {
"method": "POST",
"urlPath": "/endpoint"
},
"response": {
"status": 200,
"body": "{\"request\": {{{request.body}}}, \"anotherField\": \"string\" }",
"headers": {
"Content-Type": "application/json;charset=UTF-8"
},
"transformers": ["response-template"]
}
}
推荐阅读
- cypress - 赛普拉斯条件按钮单击
- java - Quest DB 无法使用带有线路协议的 Java 运行教程数据插入
- laravel - laravel 使用服务器 ID 连接到支付网关,而不是我的互联网 ID
- java - 使用 Firestore 中的 Picasso 进行图像检索
- gitlab - How to install MSbuild to build my dotnet framework project in Gitlab pipeline?
- kubernetes - 如何定义 CRD 中的字段顺序
- c - 如何在 C 中有效地找到从 1 到 n 的所有几乎完美的数字
- linux - 为什么我在 QOpenGLWidget 中播放媒体时图像总是蓝色
- simics - 为什么 firststeps.simics 和 qsp-client-core.simics 之间的 SMM 行为不同?
- c++ - Qt qrc 警告:qrc_icons.cpp:不是目录