首页 文章

AWS API网关不应用映射模板来删除具有空主体的请求

提问于
浏览
0

我有一个连接到Lambda函数的API网关 endpoints 设置 . 我为我的 endpoints 和我的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')"
}

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

当我发出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部分存在一个问题,即它无法处理空体(DELETE请求中的空体应根据HTTP规范有效) .

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

1 回答

  • 2

    您是否介意尝试以字符串格式(“{}”)传入空体?您可以尝试使用cURL来测试您的API . 有时,您可能必须在标头中传入Content-Type .

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

相关问题