默认响应标头:带代理集成的AWS API网关 [英] Default Response Headers: AWS API Gateway w/ Proxy Integration

查看:70
本文介绍了默认响应标头:带代理集成的AWS API网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Angular SPA前端,该前端通过AWS API网关与Lambda函数中托管的.Net Web API通信.此配置要求我们的API网关对Lambda使用代理整数.

We have an Angular SPA front end, which communicates through an AWS API Gateway to a .Net Web API hosted in a Lambda function. This configuration mandates that our API Gateway uses proxy intgeration with the Lambda.

通常,这很好.我们已在API中启用了CORS,正常的请求和响应会按预期进行.

Generally, this works well. We have enabled CORS in our API, and normal requests and responses flow as expected.

但是,当发生某些事情使API网关或.Net Lambda包装器中断时,例如超过了网关的(不可配置的)30秒超时,或者超过了Lambda的最大响应大小,则来自API网关的响应消息不会包含CORS标头.结果,无论实际错误如何,我们的前端都会记录一个CORS错误.

However, when something happens that breaks the API Gateway or .Net Lambda Wrapper, such as exceeding the Gateway's (non-configurable) 30-second timeout, or exceeding Lambda's max response size, the response message from the API Gateway does not contain a CORS header. As a result, regardless of the actual error, our front end registers a CORS error.

是否可以通过某种方式将API网关配置为始终返回默认的CORS标头?

Is there some way to configure the API Gateway to always return a default CORS header?

请注意,这是在代码之外发生的-在C#lambda函数中我无能为力,因为这与发生在该级别之上的错误有关.

Please note that this is happening outside of our code - there is nothing I can do inside of the C# lambda function, as this relates to errors happening above that level.

推荐答案

是的,您可以在AWS API网关级别进行设置

Yes, you can set it at AWS API Gateway Level

  1. 登录到AWS控制台,打开API网关服务,选择所需的网关.
  2. 在左侧面板上,选择网关响应"(它将出现在您选择的网关下方)
  3. 现在在右侧,选择默认5XX"
  4. 为Cors添加默认标头,例如Access-Control-Allow-Header,Access-Control-Allow-Methods,Access-Control-Allow-Origin
  5. 保存更改并重新部署网关.

请参考此图片以导航至所需部分

希望有帮助.

这篇关于默认响应标头:带代理集成的AWS API网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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