【问题标题】:AWS API Gateway Not Applying Mapping Template to DELETE request with empty bodyAWS API Gateway 未将映射模板应用于具有空正文的 DELETE 请求
【发布时间】:2016-06-28 02:46:36
【问题描述】:

我有一个连接到 Lambda 函数的 API 网关端点设置。我为端点和我的 lambda 函数之间的集成配置了一个映射模板,如下所示:

{  
   "httpMethod":"$context.httpMethod",
   "body":"$input.json('$')",
   "queryParams":"$input.params().querystring",
   "headerParams":"$input.params().header",
   "headerParamNames":"$input.params().header.keySet()",
   "contentTypeValue":"$input.params().header.get('Content-Type')",
   "cognitoIdentityId":"$context.identity.cognitoIdentityId",
   "cognitoIdentityPoolId":"$context.identity.cognitoIdentityPoolId",
   "id":"$input.params('id')"
}

我设置了一个/{id}路径,支持GET和DELETE,都配置了上面的映射模板。

当我发出 GET 请求时,我可以在 CloudWatch 日志中看到 API 网关获取空请求正文,处理我的映射,并将转换后的请求正文发送到我的 lambda,并填写所有预期信息。

Method request body before transformations: null
....
Endpoint request body after transformations: {"httpMethod":"GET","body":{},"queryParams":"{}","headerParams":"{....

当我向同一资源发出 DELETE 请求时,我看到了不同的行为:

Method request body before transformations: null
....
Endpoint request body after transformations: null

因此,API 网关的 DELETE 部分似乎存在问题,因为它无法处理空正文(根据 HTTP 规范,DELETE 请求中的空正文应该是有效的)。

如果我将一个空主体传递给 DELETE(例如,将主体设置为 {}),这一切似乎都可以正常工作。但是,我无法看到 API Gateway JavaScript SDK 传递一个空的 JSON 对象;如果我将生成的删除方法传递给一个空主体,它只会将 DELETE 请求的主体设置为 null 而不是空的 JSON 对象。

【问题讨论】:

    标签: amazon-web-services aws-api-gateway


    【解决方案1】:

    您介意尝试以字符串格式(“{}”)传递一个空正文吗? 您可以尝试使用 cURL 来测试您的 API。有时,您可能需要在标头中传入 Content-Type。

    我希望这些可以帮助您调试您的 API。

    【讨论】:

    • Apig.subscriptionsIdDelete({id: this.subscription.id}, "{}") 难以置信这真的有效.... :|
    猜你喜欢
    • 2018-01-20
    • 2017-11-30
    • 2016-01-21
    • 2020-09-28
    • 1970-01-01
    • 2021-04-20
    • 2018-02-03
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多