2016-02-05 22 views
1

我正试图在部署脚本中使用aws-cli从命令行设置aws CORS。 我使用以下perl命令来创建POST资源。 我试图设置集成响应'*'很像启用核心会做的。api网关CORS设置

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": "'*'"}'

我碰到下面的错误。无效的映射表达式规定:验证结果:警告:[],错误:指定了无效的映射表达式:*]

谁能告诉我这是什么

客户端错误(BadRequestException)调用PutIntegrationResponse操作时发生错误实际上是指api网关部署脚本,甚至是更好的方法。

回答

1

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

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

2

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

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

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

+0

太棒了,很高兴它解决了! –