API 网关新增(测试版)http api [英] API Gateway new (beta) http api
问题描述
我使用 API 网关创建了一个示例 HTTP API(目前处于测试版).此 API 不使用任何身份验证,并具有 lambda 作为集成.该路由接受任何 HTTP 方法,并且我已确认 lambda 具有正确的 API 网关权限.这个权限是在我创建 API 时添加的.
I have created a sample HTTP API (which is currently in a beta release) using the API gateway. This API does not use any authentication and has a lambda as an integration. The route accepts any HTTP method and I have confirmed that the lambda has the proper API gateway permission. This permission was added when I created the API.
但是,当我调用 API 时,我收到一个 HTTP 状态 500 和一个正文:{"message":"Internal Server Error"}.
However, when I call the API I receive an HTTP status of 500 and a body of: {"message":"Internal Server Error"}.
如果我将其设置为 REST API 而不是 HTTP API,则相同的 lambda 和 API 将起作用.
This same lambda and API will work if I set it up as a REST API rather than an HTTP API.
知道为什么这在 HTTP API 中不起作用吗?
Any ideas why this isn't working in the HTTP API?
推荐答案
我遇到了与 Internal Server Error
类似的问题.我正在使用带有 nodejs lambda 集成的新 API Gateway HTTP API(测试版).正如您所指出的,lambda 本身可以正常工作,而 REST API 也可以正常工作.
I had a similar issue with Internal Server Error
. I was using the new API Gateway HTTP API (beta) with a nodejs lambda integration. The lambda on its own worked and the REST API worked fine, like you noted.
花费我 8 小时的 2 个问题:
The 2 issues that cost me 8 hours:
我必须向处理程序添加一个
async
前缀或使用回调,但我不能在没有async
的情况下只返回响应.请参阅 https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
I had to add an
async
prefix to the handler or use a callback, but I couldn't just return a response withoutasync
. See https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
响应结构必须正确,请参阅此页面:https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-create-lambda-backend,滚动到示例函数代码,你会看到这个:
The response structure has to be correct, see this page: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-create-lambda-backend, scroll to the example function code and you'll see this:
// The output from a Lambda proxy integration must be
// in the following JSON object. The 'headers' property
// is for custom response headers in addition to standard
// ones. The 'body' property must be a JSON string. For
// base64-encoded payload, you must also set the 'isBase64Encoded'
// property to 'true'.
所以我的函数最终看起来像这样,注意 async
和正确的响应结构:
So my function ended up looking like this, note the async
and the proper response structure:
exports.handler = async function(event, context) {
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!')
};
return response;
}
这篇关于API 网关新增(测试版)http api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!