【问题标题】:put-gateway-response fails when --response-parameters option is set设置 --response-parameters 选项时 put-gateway-response 失败
【发布时间】:2018-03-16 00:02:56
【问题描述】:

我正在尝试使用 aws cli 设置一些自定义 API 网关响应。这是我正在使用的命令(仅相关参数):

aws apigateway put-gateway-response --response-parameters method.response.header.Access-Control-Allow-Origin='"'"'*'"'"'

完整的命令是:

aws apigateway put-gateway-response --rest-api-id w1s3nc4dxd --response-type UNAUTHORIZED --status-code 401 --response-parameters method.response.header.Access-Control-Allow-Origin=' "'"'*'"'"' --response-templates '{ "application/json": "{\"errorcode\":401,\"message\":$context.error.messageString}" }' - -region eu-west-1

它失败了:

调用 PutGatewayResponse 操作时发生错误 (BadRequestException):指定的映射表达式无效:验证结果:警告:[],错误:[指定的映射表达式无效:method.response.header.Access-Control-Allow-Origin]

如果在没有该参数的情况下执行,一切正常。我也试过json格式,结果一样。

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

有什么见解吗?提前致谢。

--- 编辑

只是为了进一步澄清。所有类型的响应参数都会失败,这不仅涉及 Access-Control-Allow-Origin 标头。

【问题讨论】:

  • 您是否启用了 CORS?
  • 好吧,我不太确定为什么 CORS 与此相关。我的一些端点启用了 CORS,而另一些则没有。只是试图为 API 网关响应设置一个标头。为了更清楚,无论响应参数是什么,它都会失败。

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


【解决方案1】:

我认为网关响应参数的格式应该是gatewayresponse.header.[name]。那将是映射键(目标),值(源)是静态值(就像您拥有的那样)或映射表达式到 method.request.(path|querystring|header).[name]stageVariables.[name]context.[name]

【讨论】:

    【解决方案2】:

    尝试使用 --response-parameters '{"gatewayresponse.header.Access-Control-Allow-Origin":"'"'*'"'"}',它对我有用

    【讨论】:

      猜你喜欢
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      相关资源
      最近更新 更多