首页 文章

AWS API网关OPTIONS请求返回500错误

提问于
浏览
2

我试图将基于Node.js Express的API部署到lambda . 我正在使用awslabs的aws-serverless-express example . 因此,我的大部分AWS配置都是为我自动创建的 .

看起来我的API通过API网关正常工作 . 我的帖子和get方法运行正常 . 但是,我需要支持CORS . 我的应用程序应该返回对OPTIONS请求的正确CORS响应,但它不适用于AWS .

最终,无论我做什么,我都会收到500份选项请求 . 我无法弄清楚如何获得有关这500个错误的任何信息 . 我不确定是什么导致了他们 .

这是500响应的主体 {"message": "Internal server error"} .

这些是响应标头:

content-length:36 content-type:application / json date:Sun,09 Jul 2017 17:56:24 GMT状态:500 via:1.1 9af17e5a616bfc9ac07fc7e415ade9e6.cloudfront.net(CloudFront)x-amz-cf-id:1_AZmkLqf1rjkog2MRtvcBAe54aIZdPWmNApBTwG48Af- v_g9WHkZw == x-amzn-requestid:ec216a62-64cf-11e7-ad2b-4f1e96508dba x-cache:来自cloudfront的错误

我很确定我的OPTIONS请求甚至没有到达Lambda上的应用程序 .

我尝试使用API网关(以及我的应用程序)配置CORS . 我正在尝试将其配置为允许所有来源 .

如果有什么我可以看到或正在调试这个问题?

编辑:

为了尝试调试此问题,我尝试在CloudWatch中为API网关启用日志记录 .

CloudWatch configuration for the API Gateway

在这之后,我在CloudWatch中看到这两个网关外观日志:

enter image description here

我一直在使用prod所以我点击该链接看到这个:

enter image description here

我假设这是一个很长的日志条目列表 . 我不确定“流”在这种情况下意味着什么 . 这些条目有数百种 . 因此,我选择具有最新时间戳的那个并单击它 . 现在我看到了这个:

enter image description here

似乎我的所有网关日志都是这样的 . IE:显然是空的 .

那么,我正确设置日志记录吗?我在正确的地方寻找吗?

4 回答

  • 0

    我刚刚完成了这个...隐藏在AWS lambda docs中启用CORS的事实是你必须在lambda中设置CORS头 . 所以这是如何做到这一点:

    let payload = {
        statusCode: 400,
        body: JSON.stringify('body'),
        headers: {"Access-Control-Allow-Origin": "*"} // NEED this for API CORS access
    };
    callback(null, payload);
    

    您必须返回有效的statusCode和正文以及 Headers ,否则API将无法将您的lambda响应转换为API响应 .

  • 0

    在使用cloudformation创建无服务器解决方案时遇到了同样的问题 . Cloudformation创建API Gateway Lambda,一切看起来都不错 . 但OPTIONS请求返回内部服务器错误 .

    我不得不去API Gateway并手动发布我的API一次 . (虽然它已经发布到prod阶段)然后OPTIONS请求工作 . 我现在可以使用cloudformation来更新 .

  • -1

    对于仍然面临aws-serverless-express的OPTIONS 500错误的人来说,解决办法是在Swagger template中将 contentHandling: CONVERT_TO_TEXT 添加到OPTIONS方法

    这是包含 //{proxy+} 路径的swagger模板文件

    ---
    swagger: 2.0
    info:
      title: AwsServerlessExpressApi
    basePath: /prod
    schemes:
    - https
    paths:
      /:
        x-amazon-apigateway-any-method:
          produces:
          - application/json
          responses:
            200:
              description: 200 response
              schema:
                $ref: "#/definitions/Empty"
          x-amazon-apigateway-integration:
            responses:
              default:
                statusCode: 200
            uri: arn:aws:apigateway:YOUR_AWS_REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:YOUR_AWS_REGION:YOUR_ACCOUNT_ID:function:${stageVariables.ServerlessExpressLambdaFunctionName}/invocations
            passthroughBehavior: when_no_match
            httpMethod: POST
            type: aws_proxy
        options:
          consumes:
          - application/json
          produces:
          - application/json
          responses:
            200:
              description: 200 response
              schema:
                $ref: "#/definitions/Empty"
              headers:
                Access-Control-Allow-Origin:
                  type: string
                Access-Control-Allow-Methods:
                  type: string
                Access-Control-Allow-Headers:
                  type: string
          x-amazon-apigateway-integration:
            contentHandling: CONVERT_TO_TEXT
            responses:
              default:
                statusCode: 200
                responseParameters:
                  method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
                  method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
                  method.response.header.Access-Control-Allow-Origin: "'https://example.com'"
            passthroughBehavior: when_no_match
            requestTemplates:
              application/json: "{\"statusCode\": 200}"
            type: mock
      /{proxy+}:
        x-amazon-apigateway-any-method:
          produces:
          - application/json
          parameters:
          - name: proxy
            in: path
            required: true
            type: string
          responses: {}
          x-amazon-apigateway-integration:
            uri: arn:aws:apigateway:YOUR_AWS_REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:YOUR_AWS_REGION:YOUR_ACCOUNT_ID:function:${stageVariables.ServerlessExpressLambdaFunctionName}/invocations
            httpMethod: POST
            type: aws_proxy
        options:
          consumes:
          - application/json
          produces:
          - application/json
          responses:
            200:
              description: 200 response
              schema:
                $ref: "#/definitions/Empty"
              headers:
                Access-Control-Allow-Origin:
                  type: string
                Access-Control-Allow-Methods:
                  type: string
                Access-Control-Allow-Headers:
                  type: string
          x-amazon-apigateway-integration:
            contentHandling: CONVERT_TO_TEXT
            responses:
              default:
                statusCode: 200
                responseParameters:
                  method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
                  method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
                  method.response.header.Access-Control-Allow-Origin: "'https://example.com'"
            passthroughBehavior: when_no_match
            requestTemplates:
              application/json: "{\"statusCode\": 200}"
            type: mock
    x-amazon-apigateway-binary-media-types:
      - '*/*'
    definitions:
      Empty:
        type: object
        title: Empty Schema
    

    更多信息可以在GitHub上找到Internal server error when request method OPTIONS

    希望这可以帮助!

  • 0

    您可以在路径索引文件中使用以下代码

    app.options(function(req,res){res.send(200);})
    

    这里app是express的路由器对象 . 这会将所有选项请求发送到200 .

相关问题