A web-based partner portal hosted on `https://partner.datasync.io` receives a `502 Bad Gateway` error and a CORS block message in the browser console when sending a `PATCH` request to an Amazon API Gateway REST API. The API is configured with a Lambda Proxy integration. The developer checks the Amazon CloudWatch logs and confirms that the backend Lambda function executed successfully and returned the following JSON structure:
{
"statusCode": 200,
"body": "{\"message\": \"Update successful\"}"
}
Which action should the developer take to resolve this error?
- Modify the Lambda function response to include the Access-Control-Allow-Origin header in a headers object within the returned JSON.Cevap
- BConfigure a CORS rule in the Amazon S3 bucket hosting the partner portal to allow the domain of the Amazon API Gateway endpoint.
- CEnable CORS in the Amazon API Gateway console for the PATCH resource, which automatically injects the Access-Control-Allow-Origin header into the Lambda function's payload.
- DModify the integration response in the Amazon API Gateway console to map the Access-Control-Allow-Origin header to the client domain.
Cevap
Modify the Lambda function response to include the Access-Control-Allow-Origin header in a headers object within the returned JSON.
In a Lambda Proxy integration, API Gateway expects the backend Lambda function to return a structured JSON response that includes status code, headers, and body. Because the integration bypasses API Gateway's integration response mappings, the Lambda function is solely responsible for returning the `Access-Control-Allow-Origin` header in its response. Without this header, the browser blocks the response, leading to a CORS policy violation and a client-side error.
Adım Adım Çözüm
Anahtar Kavram
Handling CORS and response formatting in API Gateway Lambda Proxy integrations.