如何 return 使用 Wiremock 在响应字段中请求正文

How to return request body in a field in response using 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: (

编辑:-

我使用的 wiremock 2.19.0 版本导致了这个问题。我将版本升级到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"]
  }
}