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

如何仅在API Gateway中根据请求Content-Type适配响应体格式?

How to Make API Gateway Automatically Adapt Response Format to Request Content-Type

Nice question! Since you’ve already got request mapping templates set up for both application/json and application/xml, getting API Gateway to automatically return the right response format based on the client’s Content-Type header is totally achievable with response mapping templates and content negotiation. Let’s walk through exactly how to set this up:

Step 1: Ensure Lambda Returns Structured JSON

First off, keep your Lambda function simple. No matter what format the client requests, have Lambda return a standard, structured JSON object. For example:

{
  "status": "success",
  "data": {
    "id": 123,
    "name": "Sample Data"
  }
}

API Gateway will handle converting this JSON to XML (or keeping it as JSON) based on the client’s request.

Step 2: Configure Response Body Mapping Templates

Head over to your API Gateway method’s Integration Response tab (this is where we define how to transform Lambda’s output into the client’s desired format):

  1. Open your target API and navigate to the specific method (e.g., POST/GET) you’re working with.
  2. Switch to the Integration Response tab.
  3. Expand the HTTP status code you want to configure (most likely 200 for successful responses).
  4. In the Body Mapping Templates section, click Add mapping template:
    • For JSON responses: Enter application/json as the Content-Type. The template can be as simple as $input.path('$')—this just passes through Lambda’s JSON output directly.
    • For XML responses: Enter application/xml as the Content-Type. Write a template that converts Lambda’s JSON into valid XML. Here’s an example matching the JSON above:
      <response>
        <status>$input.path('$.status')</status>
        <data>
          <id>$input.path('$.data.id')</id>
          <name>$input.path('$.data.name')</name>
        </data>
      </response>
      
    Adjust the XML structure to match whatever data your Lambda returns.

Step 3: Dynamically Match Response Content-Type

This is the key step to make the adaptation automatic:

  1. Switch to the Method Response tab of your API method.
  2. Expand the same HTTP status code (e.g., 200) you configured earlier.
  3. Find the Content-Type header under Response Headers for 200 and set its value to $context.requestContentType.

This tells API Gateway to use the exact Content-Type from the client’s request as the response’s Content-Type header, and automatically pick the corresponding response mapping template you set up in Step 2.

Step 4: Test It Out

Deploy your API changes, then run a couple test requests:

  • Send a request with Content-Type: application/json—you’ll get a JSON response with the matching Content-Type header.
  • Send a request with Content-Type: application/xml—you’ll get your custom XML response, again with the correct Content-Type header.

Extra Tips

  • If clients send Content-Type headers with extra parameters (like application/json; charset=utf-8), use $util.parseMediaType($context.requestContentType).baseType instead of $context.requestContentType in your Method Response. This extracts just the base type (e.g., application/json) to match your template names.
  • Add a default mapping template if you want to handle unrecognized Content-Type values—this will act as a fallback (e.g., return JSON by default).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:43:39