我正在使用 SAM 在 cloudformation 中创建 API。
问题: 在我的 SAM 应用程序模板中,我应该在哪里添加“X-Requested-With”标头?如何编辑我的代码以使预检成功而无需进入 AWS 控制台?
问题: 我正在处理我的预检请求,当我部署我的 SAM 应用程序时,我在邮递员中得到 403 FORBIDDEN,当我转到 AWS 控制台时,标题“X-Requested-With”不存在。
如果我尝试在控制台中添加“X-Requested-With”标头,它仍然会给出错误,但是如果我在控制台中删除 OPTIONS 方法并根据(https://enable )从头开始创建 OPTIONS 方法-cors.org/server_awsapigateway.html)它有效,我在 postman 中得到 200 OK。
代码:
模板.yaml
Globals:
Function:
Timeout: 10
Api:
Cors:
AllowMethods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
AllowHeaders: "'Content-Type,X-Amz-Date,X-Amz-Security-Token,Authorization,X-Api-Key,X-Requested-With,Accept,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Access-Control-Allow-Headers'"
AllowOrigin: "'*'"
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
ApiKeyRequired: true
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: mypath/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Events:
KrySeisoen:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /mypath
Method: get
Policies:
- ...
VpcConfig:
SecurityGroupIds:
- ...
SubnetIds:
- ...