【问题标题】:Enable CORS in AWS API Gateway with aws-cli使用 aws-cli 在 AWS API Gateway 中启用 CORS
【发布时间】:2019-03-08 02:27:16
【问题描述】:

我目前正在编写脚本,以便在将资源添加到 AWS API Gateway 上的 API 端点后以编程方式启用 CORS。在探索了几个小时的 put-integration-response 函数之后。我几乎取得了突破,但我遇到了一个错误:

An error occurred (BadRequestException) when calling the 
PutIntegrationResponse operation: Invalid mapping expression specified: 
Validation Result: warnings : [], errors : [No method response exists 
for method.]

这是我用来启用 CORS 的脚本:

aws apigateway put-integration-response --rest-api-id XXXXX --resource 
-id XXXX --http-method GET --status-code 200 --selection-pattern 200 -- 
response-parameters '{"method.reponse.header.Access-Control-Allow- 
Origin": "'"'*'"'", "method.response.header.Access-Control-Allow- 
Headers": "'"'integration.request.header.Authorization'"'"}'

我发现的奇怪之处是 AWS 文档似乎与当前版本的 aws-cli 过时了。我花了几个小时来解决我在调用 api 时遇到的一些基本问题。

将不胜感激任何想法。

干杯! 呐

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway aws-serverless


    【解决方案1】:

    在您的 aws apigateway put-integration-response 的 AWS CLI 命令中发现了几个问题

    1. 有一个拼写错误

    method.reponse.header.Access-Control-Allow-Origin

    一定是:

       method.response.header.Access-Control-Allow-Origin
    
    1. 要将值“*”设置为Access-Control-Allow-Origin,您需要使用"'"'"'*'"'"'" 而不是"'"'*'"'" 在响应参数中可以设置method.reponse.header.Access-Control-Allow-Origin,但不能设置method.response.header.Access-Control-Allow-Headers
    2. 错误原因

    PutIntegrationResponse 操作:指定的映射表达式无效

    是因为你试图在response-parameters中设置method.response.header.Access-Control-Allow-Headers

    下面应该是最终的 AWS CLI 命令

    aws apigateway put-integration-response --rest-api-id XXXXX --resource-id XXXX --http-method GET --status-code 200 --selection-pattern 200 
    --response-parameters '{"method.response.header.Access-Control-Allow-Origin": "'"'"'*'"'"'"}'
    

    【讨论】:

    • method.response.header.Access-Control-Allow-Origin(缺少 s)
    猜你喜欢
    • 2021-09-15
    • 2021-03-08
    • 2020-02-20
    • 2020-08-17
    • 2019-12-30
    • 2018-02-21
    • 2017-09-19
    • 2019-07-09
    • 1970-01-01
    相关资源
    最近更新 更多