使用aws-cli在AWS API Gateway中启用CORS [英] Enable CORS in AWS API Gateway with aws-cli
问题描述
我目前正在编写脚本,以在将资源添加到AWS API Gateway上的API端点后以编程方式启用CORS.在研究了放置积分响应功能达数小时之后.我几乎取得了突破,但是这是我遇到的错误:
I'm currently writing script to programmatically enable CORS once a resource is added to an API Endpoint on AWS API Gateway. After exploring the put-integration-response function for hours. I almost got a breakthrough, but here is an error I'm getting:
An error occurred (BadRequestException) when calling the
PutIntegrationResponse operation: Invalid mapping expression specified:
Validation Result: warnings : [], errors : [No method response exists
for method.]
这是我用来启用CORS的脚本:
Here is the script I'm using to enable 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调用遇到的一些基本问题.
The weird thing I found was the AWS documentation seems to be out of date with the current version of the aws-cli
It tooks me hours to fix some basic issues I had with the api call.
将对任何想法表示感谢.
Will be grateful for any ideas.
干杯!Nyah
推荐答案
在您的AWS CLI命令中发现的 aws apigateway put-integration-response
Couple of issues found in your AWS CLI command for aws apigateway put-integration-response
- 有错别字
method.reponse.header.Access-Control-Allow-Origin
method.reponse.header.Access-Control-Allow-Origin
它必须是:
method.response.header.Access-Control-Allow-Origin
- 要为
Access-Control-Allow-Origin
设置值'*',您需要改用'"'"**"""
的'"'*''"
在响应参数中,您可以设置method.reponse.header.Access-Control-Allow-Origin
,但不能设置method.response.header.Access-Control-Allow-Headers 代码>
- 错误原因
- To set a value '*' to
Access-Control-Allow-Origin
you need to use"'"'"'*'"'"'"
instead of"'"'*'"'"
In response-parameters you can setmethod.reponse.header.Access-Control-Allow-Origin
, but can not setmethod.response.header.Access-Control-Allow-Headers
- The reason of the error
PutIntegrationResponse操作:指定了无效的映射表达式
PutIntegrationResponse operation: Invalid mapping expression specified
是因为您试图在 response-parameters
下面应该是最终的AWS CLI命令
Below should be the final AWS CLI command
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": "'"'"'*'"'"'"}'
这篇关于使用aws-cli在AWS API Gateway中启用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!