在AWS Api Gateway中配置CORS,以便在OPTIONS请求中返回CORS头。为此,可以使用API Gateway控制台或AWS CLI。
以下是在API Gateway控制台中配置CORS的步骤:
以下是使用AWS CLI在API Gateway中配置CORS的示例命令:
aws apigateway update-rest-api \
--rest-api-id your_rest_api_id \
--patch-operations op=replace,path=/cors/enabled,value=true
aws apigateway put-method-response \
--rest-api-id your_rest_api_id \
--resource-id your_resource_id \
--http-method OPTIONS \
--status-code 200 \
--response-models '{"application/json":"Empty"}' \
--response-parameters "method.response.header.Access-Control-Allow-Methods=true,method.response.header.Access-Control-Allow-Headers=true"
aws apigateway put-integration-response \
--rest-api-id your_rest_api_id \
--resource-id your_resource_id \
--http-method OPTIONS \
--status-code 200 \
--selection-pattern "" \
--response-parameters "method.response.header.Access-Control-Allow-Methods=true,method.response.header.Access-Control-Allow-Headers=true"
aws apigateway update-stage \
--rest-api-id your_rest_api_id \
--stage-name your_stage_name \
--patch-operations \
op=replace,path=/settings/methodSettings/*/metrics/enabled,value=true \
op=replace,path=/settings/methodSettings/*/loggingLevel,value=INFO
这将启用CORS,设置必需的Header和Method,以便在OPTIONS请求中返回CORS头。