【问题标题】:api gateway CORS setupapi网关CORS设置
【发布时间】:2016-05-15 16:28:17
【问题描述】:

我正在尝试在部署脚本中使用 aws-cli 从命令行设置 aws CORS。 我已经使用以下 perl to shell 命令创建了 POST 资源。 我正在尝试将集成响应设置为“*”,就像启用内核一样。

aws apigateway put-method-response \\ --region "$region" \\ --rest-api-id "$api_id" \\ --resource-id "$resource_id" \\ --http-method "POST" \\ --status-code 200 \\ --response-models '{"application/json":"Empty"}' \\ --response-parameters '{"method.response.header.Access-Control-Allow-Origin":true}'

当我运行以下命令来设置积分值时。

aws apigateway put-integration-response \\ --region "$region" \\ --rest-api-id "$api_id" \\ --resource-id "$resource_id" \\ --http-method "$method" \\ --status-code 200 \\ --response-template '{"application/json":"Empty"}' \\ --response-parameters \\ '{"method.response.header.Access-Control-Allow-Origin": "'*'"}'

我收到以下错误。

调用 PutIntegrationResponse 操作时发生客户端错误 (BadRequestException):指定的映射表达式无效:验证结果:警告:[],错误:[指定的映射表达式无效:*]

谁能告诉我这个错误到底指的是什么,甚至是更好的方法来处理 api 网关部署脚本。

【问题讨论】:

    标签: json perl amazon-web-services aws-cli aws-api-gateway


    【解决方案1】:

    问题最终成为转义字符的正确方法。

    --response-parameters '{"method.response.header.Access-Control-Allow-Origin":"'"'*'"'"}'

    显然,这更多是关于了解格式化 JSON 的正确方法。

    【讨论】:

      【解决方案2】:

      API Gateway 管理控制台有一个不错的“启用 CORS”功能,您可能已经看到了。至于使用 CLI 进行复制,我建议使用控制台功能,然后观察配置,并在 CLI 请求中使用相同的参数。

      您看到的错误一定是由于值 '*' 的单引号转义不正确造成的,因为只有字符 * 无效。还要指出另一个潜在问题, --response-templates '{"application/json":"Empty"}' 的输入是有效的,但与方法响应对象中的 --response-models 的解释不同.对于使用该集成响应的所有 API 调用,该值会将响应正文设置为“空”。要进行透传,只需不要设置 --response-templates 的值

      【讨论】:

      • 当您通过控制台启用 cors 时。它向端点添加了一个选项方法。想知道这是否真的需要?我从不需要在 node.js 网络应用程序中添加选项方法。
      猜你喜欢
      • 2020-07-04
      • 2020-07-16
      • 1970-01-01
      • 2021-08-26
      • 2016-05-18
      • 2017-09-16
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多