You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WireMock映射JSON配置求助:响应展示请求头及自定义内容

WireMock Mapping Configuration: Display All Request Headers & Add Custom Content

Hey there! Let's fix your WireMock mapping issues and get it working exactly how you want.

1. Display Multiple/All Request Headers in the Response

Right now, your mapping only pulls the first value of the msisdn header. To include additional headers (or all headers), you have two straightforward options:

Option A: Show Specific Headers

If you want to explicitly include specific headers (like msisdn and another one, say user-agent), you can concatenate them in the response body. For plain text output:

{
  "request": {
    "urlPath": "/templated"
  },
  "response": {
    "body": "MSISDN: {{request.headers.msisdn.[0]}}\nUser-Agent: {{request.headers.user-agent.[0]}}",
    "transformers": ["response-template"]
  }
}

For structured JSON output (make sure to set the Content-Type header):

{
  "request": {
    "urlPath": "/templated"
  },
  "response": {
    "body": "{\"msisdn\": \"{{request.headers.msisdn.[0]}}\", \"userAgent\": \"{{request.headers.user-agent.[0]}}\"}",
    "headers": {
      "Content-Type": "application/json"
    },
    "transformers": ["response-template"]
  }
}

Option B: Show All Request Headers

To dump all incoming request headers as a JSON object, use the {{request.headers}} variable directly:

{
  "request": {
    "urlPath": "/templated"
  },
  "response": {
    "body": "{{request.headers}}",
    "headers": {
      "Content-Type": "application/json"
    },
    "transformers": ["response-template"]
  }
}

2. Add Custom Values/Text to the Response Body

Absolutely! You can mix static custom text, hardcoded values, and even other WireMock template variables right into the response body. Here are a few practical examples:

Example 1: Static Custom Text + Headers

{
  "request": {
    "urlPath": "/templated"
  },
  "response": {
    "body": "Hello! Your MSISDN is {{request.headers.msisdn.[0]}}\nThis is a custom message just for you.",
    "transformers": ["response-template"]
  }
}

Example 2: Custom JSON with Headers + Static Fields

{
  "request": {
    "urlPath": "/templated"
  },
  "response": {
    "body": "{\"status\": \"success\", \"msisdn\": \"{{request.headers.msisdn.[0]}}\", \"customId\": \"98765\", \"message\": \"Request processed successfully\"}",
    "headers": {
      "Content-Type": "application/json"
    },
    "transformers": ["response-template"]
  }
}

Example 3: Use WireMock's Built-in Template Variables

You can also leverage dynamic variables like the current timestamp or request URL:

{
  "request": {
    "urlPath": "/templated"
  },
  "response": {
    "body": "Request received at {{now}} for URL {{request.url}}\nYour MSISDN: {{request.headers.msisdn.[0]}}",
    "transformers": ["response-template"]
  }
}

Just remember to keep the response-template transformer enabled—it's what makes all these template substitutions work smoothly.

内容的提问来源于stack exchange,提问作者Sebastian Diaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:35:20