可以使用 AWS API 网关的配置选项来解决此问题。需要将 "Require Content-Length header" 配置选项设置为 false,该选项默认为 true。这可以通过以下代码进行设置:
aws apigateway update-rest-api --rest-api-id
其中,
另外,也可以在 Lambda 函数的代码中添加以下代码片段来防止出现此问题:
if event["httpMethod"] == "GET" and "Content-Length" not in event["headers"]: event["headers"]["Content-Length"] = "0"
在这个代码片段中,检查请求方法是否为 GET,并验证请求头中是否包含 Content-Length。如果此值不存在,则将其设置为 0。
这些方法将确保在没有请求主体的 GET 请求时不会返回 HTTP 413 错误。