api网关CORS设置

时间:2016-02-05 16:04:28

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

我正在尝试使用部署脚本中的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网关部署脚本。

2 个答案:

答案 0 :(得分:4)

这个问题最终成为逃脱角色的正确方法。

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

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

答案 1 :(得分:1)

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

您看到的错误必定是由于错误地转义值'*'的单引号引起的,因为只有字符*无效。另外只是为了指出另一个潜在的问题, - response_ templates'{“application / json”:“Empty”}'的输入是有效的,但不会被解释为与方法响应对象中的--response-models相同。对于使用该集成响应的所有API调用,该值将响应主体设置为“Empty”。要做一个passthrough,只是不要设置--response-templates

的值