An engineering team is building a REST API using Amazon API Gateway that integrates with a legacy HTTP backend service using an HTTP integration (non-proxy). When the legacy backend cannot find a record, it returns an HTTP status code 404 with a plain text error message. The team wants the API Gateway to intercept this 404 response and instead return an HTTP status code 200 to the client, containing a structured JSON payload: {"available": false, "reason": "Record not found"}. Which configuration steps must the developer perform in API Gateway to meet these requirements?
- AConfigure the API Gateway method to use an HTTP Proxy integration, and set up a Gateway Response for the 404 status code to rewrite the HTTP status code to 200 and inject the custom JSON payload.
- BIn the Method Response configuration, create a response mapping rule with a regex of 404 and specify the custom JSON mapping template. In the Integration Response configuration, add a method response with a status code of 200.
- In the Method Response configuration, ensure the 200 status code is defined. In the Integration Response configuration, create a mapping rule with an HTTP status regex of 404, map it to the 200 method response, and define an application/json mapping template to output the custom JSON payload.Answer
- DConfigure an Amazon Cognito User Pool authorizer to intercept the legacy HTTP backend's response, validate the OAuth scopes, and transform the 404 status code into a 200 status code with the custom JSON payload.
Answer
In the Method Response configuration, ensure the 200 status code is defined. In the Integration Response configuration, create a mapping rule with an HTTP status regex of 404, map it to the 200 method response, and define an application/json mapping template to output the custom JSON payload.
To transform a backend response's status code and body in a non-proxy integration, you must first define the target status code (200) in the Method Response. Then, in the Integration Response, you map the backend's response status code (404) to the target Method Response status code (200) and specify a mapping template to output the required JSON format. This decouples the client-facing API contract from the legacy backend implementation.
Step-by-Step Solution
Key Concept
API Gateway Integration Response Mapping
Estimated Time:2m 0s