将事件从API网关传递到Lambda [英] Passing event from API gateway to Lambda

查看:62
本文介绍了将事件从API网关传递到Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个lambda函数,可以接收许多不同形式的JSON.这个想法是通过API网关接收的.当我测试给定任何形式的JSON的函数时,它工作正常.但是,当我尝试通过API网关对其进行测试时,它失败了,因为该事件为空.

I have a lambda function that can received JSON in many different forms. The idea is to receive that through API Gateway. When i test the function given any form of JSON, it works fine. However, when i try to test it through API Gateway, it fails because the event is empty.

我试图添加一个空的模型映射:

I tried to add an empty model mapping:

{}

但是我仍然得到相同的结果.如何使API将主体中的任何JSON类型传递给函数?

But i still get the same result. How can I make the API pass any type of JSON in the body to the function?

谢谢!

编辑

我想使用Lambda代理集成,并且在AWS控制台中使用邮差和API网关测试功能进行了测试.我只是在主体中包含所需JSON和内容类型标头的地方进行POST.

I want to use Lambda Proxy integration and i have testing with postman and the API gateway test feature in the AWS console. I'm just doing a POST with the required JSON in the body and the content type headers.

推荐答案

您不需要响应模型.

在API网关中使用lambda代理时,您会以特定形式获取数据(

When you use a lambda proxy in API gateway you get the data in specific form (https://serverless.com/framework/docs/providers/aws/events/apigateway/#example-lambda-proxy-event-default) for full details.

不同于直接调用 handler(event,ctx,callback)函数的位置,该函数使您可以简单地执行 const payload = event 来获取要测试的数据;您将需要执行 const payload = JSON.parse(event.body),因为 event 参数包含有关完整http请求的大量信息.

Unlike invoking directly where the handler(event, ctx, callback) function allows you to simply do const payload = event to get the data you were testing with; you will need to do const payload = JSON.parse(event.body), as the event argument contains significantly more information about the full http request.

这篇关于将事件从API网关传递到Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆