如何仅在API Gateway中根据请求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):
- Open your target API and navigate to the specific method (e.g., POST/GET) you’re working with.
- Switch to the Integration Response tab.
- Expand the HTTP status code you want to configure (most likely 200 for successful responses).
- In the Body Mapping Templates section, click Add mapping template:
- For JSON responses: Enter
application/jsonas 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/xmlas 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>
- For JSON responses: Enter
Step 3: Dynamically Match Response Content-Type
This is the key step to make the adaptation automatic:
- Switch to the Method Response tab of your API method.
- Expand the same HTTP status code (e.g., 200) you configured earlier.
- Find the
Content-Typeheader 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 matchingContent-Typeheader. - Send a request with
Content-Type: application/xml—you’ll get your custom XML response, again with the correctContent-Typeheader.
Extra Tips
- If clients send
Content-Typeheaders with extra parameters (likeapplication/json; charset=utf-8), use$util.parseMediaType($context.requestContentType).baseTypeinstead of$context.requestContentTypein your Method Response. This extracts just the base type (e.g.,application/json) to match your template names. - Add a
defaultmapping template if you want to handle unrecognizedContent-Typevalues—this will act as a fallback (e.g., return JSON by default).
内容的提问来源于stack exchange,提问作者Radek

