繁体   English   中英

为什么在尝试通过 AWS API Gateway 和 Lambda 执行 DELETE 请求时会收到 CORS 内部服务器错误?

[英]Why do I get a CORS internal server error when trying to do a DELETE request via AWS API Gateway and Lambda?

我制作了 todo 应用程序,我可以在 lambda 函数中处理 GET、POST 方法,但是在调用 delete 方法时出现错误。这里我想通过 lambda 函数从 axios 进行删除查询来删除 dynamo db 中的数据

这是 axios 删除函数,它将{"data": {"id":this.id}}发送到 lambda

   axios.delete('https://94sc9th9bi.execute-api.ap-northeast-1.amazonaws.com/prod/item',
        { "data": {"id":this.id}}).then(
          res => {
            console.log(res.data.id)
          }).catch(err => {console.log(err)})

      this.getalltask()
    },

我有用于删除的 lambda api

const AWS = require('aws-sdk')
const docClient = new AWS.DynamoDB.DocumentClient()

exports.handler = async (event) => {
    console.log(event)
    let body = JSON.parse(event.body);
     const scanItemPayload = {
        TableName: 'aws-training',
        Key:{
         id: body.data.id
    }
     }
     console.log(body);

   
    const dynamoDBResponse = await docClient.delete(scanItemPayload).promise()
  console.log(dynamoDBResponse)

    const response = {
        body: JSON.stringify(dynamoDBResponse),
       statusCode: 200,
  headers: {
    "Access-Control-Allow-Origin" : "*", // Required for CORS support to work
    "Access-Control-Allow-Credentials" : true, // Required for cookies, authorization headers with HTTPS 
 
  },
    };
    return response;
};

我在上面测试了 lambda

{
    "body": "{\"data\":{\"id\":\"1633613467228\"}}"
}

我得到 statusCode 200 并且没有错误,我检查数据是否在 dynamo db 中被删除

我有一个与上面的 lambda 函数相关的 DELETE 方法 API,我通过给出查询 {item} => id=1633613467228 来测试上面的删除方法 API,这是我要删除的 ID

但它给了我结果

{
  "message": "Internal server error"
}


with error log

Execution log for request f83e7e01-52ca-498d-b3e6-34d972510ad8
Fri Oct 08 15:50:00 UTC 2021 : Starting execution for request: f83e7e01-52ca-498d-b3e6-34d972510ad8
Fri Oct 08 15:50:00 UTC 2021 : HTTP Method: DELETE, Resource Path: /item
Fri Oct 08 15:50:00 UTC 2021 : Method request path: {}
Fri Oct 08 15:50:00 UTC 2021 : Method request query string: {id=1633613467228}
Fri Oct 08 15:50:00 UTC 2021 : Method request headers: {}
Fri Oct 08 15:50:00 UTC 2021 : Method request body before transformations: 
Fri Oct 08 15:50:00 UTC 2021 : Endpoint request URI: https://lambda.ap-northeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:184371581740:function:aws-training-20211006-p-delete/invocations

我和邮递员一起测试

https://94sc9th9bi.execute-api.ap-northeast-1.amazonaws.com/prod/item?id=1633613467228

我有错误,似乎查询 ?id=1633613467228 不起作用

我还测试了应用程序,虽然我已经将访问控制 orign 设置为 *,但我在控制台中遇到了 CORRS/网络错误

Access to XMLHttpRequest at 'https://94sc9th9bi.execute-api.ap-northeast-1.amazonaws.com/prod/item?' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
index.vue?0f48:64 Error: Network Error
    at createError (createError.js?2d83:16)
    at XMLHttpRequest.handleError (xhr.js?b50d:117)
xhr.js?b50d:210 DELETE https://94sc9th9bi.execute-api.ap-northeast-1.amazonaws.com/prod/item? net::ERR_FAILED 502

所以我的问题是:
1.为什么上面api中的delete方法会导致内部服务器错误,我该如何测试该方法。 我也对 api 网关中的测试和 lambda 函数中的测试之间的不同感到困惑。 我的测试数据格式错误吗?

  1. 在 axios 中,我有"data": {"id":this.id}})Key:{id: body.data.id}是获取 axios 在 aws lambda 函数中发送的数据的正确方法吗?

这是亚马逊云观察的错误。 似乎“数据”为空。

START RequestId: 8197a2bb-b045-438b-8b37-4467687006e3 Version: $LATEST
2021-10-09T06:28:31.894Z    8197a2bb-b045-438b-8b37-4467687006e3    INFO    { key: { id: '1633613467228' } }
2021-10-09T06:28:31.927Z    8197a2bb-b045-438b-8b37-4467687006e3    ERROR   Invoke Error    
{
    "errorType": "SyntaxError",
    "errorMessage": "Unexpected token u in JSON at position 0",
    "stack": [
        "SyntaxError: Unexpected token u in JSON at position 0",
        "    at JSON.parse (<anonymous>)",
        "    at Runtime.exports.handler (/var/task/index.js:12:21)",
        "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
    ]
}

END RequestId: 8197a2bb-b045-438b-8b37-4467687006e3
REPORT RequestId: 8197a2bb-b045-438b-8b37-4467687006e3  Duration: 56.29 ms  Billed Duration: 57 ms  Memory Size: 128 MB Max Memory Used: 72 MB  Init Duration: 399.98 ms    
START RequestId: ff096041-a1fb-4349-abbc-a5d422e034d6 Version: $LATEST
2021-10-09T06:29:04.648Z    ff096041-a1fb-4349-abbc-a5d422e034d6    INFO    {
  resource: '/item',
  path: '/item',
  httpMethod: 'DELETE',
  headers: null,
  multiValueHeaders: null,
  queryStringParameters: { id: '1633613467228' },
  multiValueQueryStringParameters: { id: [ '1633613467228' ] },
  pathParameters: null,
  stageVariables: null,
  requestContext: {
    resourceId: '2gw7om',
    resourcePath: '/item',
    httpMethod: 'DELETE',
    extendedRequestId: 'G7V7mFskNjMF-vg=',
    requestTime: '09/Oct/2021:06:29:04 +0000',
    path: '/item',
    accountId: '184371581740',
    protocol: 'HTTP/1.1',
    stage: 'test-invoke-stage',
    domainPrefix: 'testPrefix',
    requestTimeEpoch: 1633760944483,
    requestId: 'f7596258-871a-4b15-b62c-11d434e176b4',
    identity: {
      cognitoIdentityPoolId: null,
      cognitoIdentityId: null,
      apiKey: 'test-invoke-api-key',
      principalOrgId: null,
      cognitoAuthenticationType: null,
      userArn: 'arn:aws:iam::184371581740:user/user07',
      apiKeyId: 'test-invoke-api-key-id',
      userAgent: 'aws-internal/3 aws-sdk-java/1.12.71 Linux/5.4.134-73.228.amzn2int.x86_64 OpenJDK_64-Bit_Server_VM/25.302-b08 java/1.8.0_302 vendor/Oracle_Corporation cfg/retry-mode/standard',
      accountId: '184371581740',
      caller: 'AIDASV3LHCMWIZKMZMLPE',
      sourceIp: 'test-invoke-source-ip',
      accessKey: 'ASIASV3LHCMWBJROEHQN',
      cognitoAuthenticationProvider: null,
      user: 'AIDASV3LHCMWIZKMZMLPE'
    },
    domainName: 'testPrefix.testDomainName',
    apiId: '94sc9th9bi'
  },
  body: null,
  isBase64Encoded: false
}
2021-10-09T06:29:04.667Z    ff096041-a1fb-4349-abbc-a5d422e034d6    ERROR   Invoke Error    
{
    "errorType": "TypeError",
    "errorMessage": "Cannot read property 'data' of null",
    "stack": [
        "TypeError: Cannot read property 'data' of null",
        "    at Runtime.exports.handler (/var/task/index.js:16:19)",
        "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
    ]
}

END RequestId: ff096041-a1fb-4349-abbc-a5d422e034d6
REPORT RequestId: ff096041-a1fb-4349-abbc-a5d422e034d6  Duration: 180.66 ms Billed Duration: 181 ms Memory Size: 128 MB Max Memory Used: 73 MB  
START RequestId: 1adde91a-ce53-4d2f-8fa8-d296352fc689 Version: $LATEST
2021-10-09T06:30:01.788Z    1adde91a-ce53-4d2f-8fa8-d296352fc689    INFO    { key: { id: '1633613467228' } }
2021-10-09T06:30:01.807Z    1adde91a-ce53-4d2f-8fa8-d296352fc689    ERROR   Invoke Error    
{
    "errorType": "SyntaxError",
    "errorMessage": "Unexpected token u in JSON at position 0",
    "stack": [
        "SyntaxError: Unexpected token u in JSON at position 0",
        "    at JSON.parse (<anonymous>)",
        "    at Runtime.exports.handler (/var/task/index.js:12:21)",
        "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
    ]
}

intecept axios 查看数据

我用 body 测试 lambda 函数并得到 200
用身体测试 lambda

在此处输入图片说明

我通过添加删除查询来测试 api 网关,它给出了内部服务器错误
在此处输入图片说明

我试图在这里使用 axios 发出请求,我想删除名称为“aa”且 id = 1633601975370 的任务
删除任务名称="aa"

如您所见,发送了带有id的body,但是axios请求删除api时出现错误

我在控制台中遇到的错误
控制台中的错误

amaxon cloudwatch 日志错误
在此处输入图片说明

云监控日志错误

即使我已经将Access-Control-Allow-Origin*我还是在控制台中收到了 CORS 错误

问题是:

您是否尝试为 Lambda代理集成或 Lambda非代理集成启用 CORS?

启用 CORS 将集成类型而异


首先,请参阅使用Amazon API Gateway 开发人员指南的 API Gateway 控制台部分对资源启用 CORS,因为它包括图像等。

遵循代理和非代理指南。

如果它是一个非代理集成,你就完成了。


如果它是代理集成(我认为不是),您的请求仍然会失败 - DELETE请求被 CORS 规范归类为复杂请求

这意味着,如果您使用 Web 应用程序调用此端点,您可能已允许所有来源,但您尚未指定要允许的 HTTP方法(Web 应用程序将在之前以preflight请求的形式请求) DELETE请求)。

因此,您还需要将Access-Control-Allow-Methods标头设置为*以允许在您的 Lambda 返回的响应中使用 HTTP DELETE

const response = {
    body: JSON.stringify(dynamoDBResponse),
    statusCode: 200,
    headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "*",
        "Access-Control-Allow-Credentials": true
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM