api网关CORS设置 [英] api gateway CORS setup

查看:204
本文介绍了api网关CORS设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to setup aws CORS from the command line using aws-cli in a deployment script. I have created the POST Resource with the following perl to shell command. I'm attempting to set the integration-response to '*' much like enabling cores would do.

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-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}'

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

When i run the following command to set the integration value.

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

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):指定了无效的映射表达式:验证结果:警告:[],错误:[指定了无效的映射表达式:*]

A client error (BadRequestException) occurred when calling the PutIntegrationResponse operation: Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: *]

任何人都可以告诉我此错误的真正含义,或者是实现api网关部署脚本的更好方法.

Can anyone tell me what this error is really referring to or even a better way to go about a api gateway deployment script.

推荐答案

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

The API Gateway management console has a nice 'Enable CORS' feature, which you may have seen. As far as replicating using the CLI, I'd suggest using the console feature, observing the configuration afterwards, and using the same parameters in the CLI requests.

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

The error you're seeing must be caused by incorrect escaping of the single quotes for the value '*' because just the character * would not be valid. Also just to point out another potential problem, the input to --response-templates '{"application/json":"Empty"}' is valid but is not interpreted the same as --response-models in a method-response object. That value will set the response body to "Empty" for all API calls that use that integration-response. To do a passthrough, just don't set the value of --response-templates

这篇关于api网关CORS设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆