API网关新版(测试版)http api [英] API Gateway new (beta) http api
问题描述
我已经使用API网关创建了一个示例HTTP API(当前处于beta版).此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
上遇到了类似的问题.我正在使用带有Node.js 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屋!