Soru

Zorluk: OrtaAPI Development and Integration with Amazon API Gateway

An organization enforces a standard error response schema where all HTTP error responses must return a JSON object containing a custom error ID and a descriptive message. A developer deployed a REST API in Amazon API Gateway protected by a Cognito User Pool authorizer. During testing, clients attempting to call the API without a valid token receive the default Gateway response `{"message":"Unauthorized"}`. Which configuration should the developer implement in API Gateway to return the custom error schema?

  1. Update the Gateway Response for the Unauthorized error type in API Gateway by configuring a mapping template for the application/json content type.Cevap
  2. B
    Configure an integration response mapping template on the API Gateway method to catch the 401 status code and transform the payload.
  3. C
    Replace the Cognito User Pool authorizer with a custom Lambda authorizer, and return the custom JSON error payload in the response context when denying access.
  4. D
    Enable CORS on the API Gateway resources and add the custom error response schema under the Access-Control-Expose-Headers configuration.

Cevap

Update the Gateway Response for the Unauthorized error type in API Gateway by configuring a mapping template for the application/json content type.
Updating the Gateway Response for the Unauthorized error type is the correct approach. Gateway Responses are used to customize responses generated by API Gateway itself when a request does not reach the integration backend (such as authentication or authorization failures). By configuring a mapping template for the application/json content type, developers can format the response body to match any custom JSON error schema.

Adım Adım Çözüm

1
Identify the point of failure for requests without a valid token.
Requests are blocked at the Cognito Authorizer stage in API Gateway, prior to reaching the integration backend.
Since the request is blocked by API Gateway before the backend executes, standard integration responses cannot be used to modify the error payload.
2
Determine the API Gateway mechanism designed to handle client-side or gateway-generated errors.
Identify 'Gateway Responses' as the feature that controls errors originating from API Gateway, such as 401 Unauthorized or 403 Forbidden.
Gateway Responses allow API administrators to customize the response status code, headers, and body mapping templates for gateway-level errors.
3
Configure the Gateway Response for the Unauthorized error type.
Add a mapping template for the application/json content type using Velocity Template Language (VTL) to return the custom error ID and message.
This guarantees that any time API Gateway rejects a request due to authorization failure, it formats the error response body to match the organization's required JSON schema.

Anahtar Kavram

Amazon API Gateway Gateway Responses
Tahmini Süre:1m 30s
Bu soruyu puanla