简体   繁体   English

AWS API 网关 POST 请求不适用于 Lambda function

[英]AWS API Gateway POST request is not working for Lambda function

Here is my lambda function:这是我的 lambda function:

@Override
    public List<JobData> handleRequest(Map<String,String> searchFilters, Context context) {
        List<JobData> jobs = new ArrayList<>();
        if(searchFilters.get("job_title") != null){
           // populate jobs list using a method.
        }else{
          // populate jobs list using another method.
        }
        return jobs;      
    }

I used the below test event for testing the lambda:我使用以下测试事件来测试 lambda: 在此处输入图像描述

And the lambda function is working as expected for the above mentioned test input.对于上述测试输入,lambda function 按预期工作。 在此处输入图像描述

Then, I created an API gateway with a POST request triggering the above mentioned lambda function.然后,我创建了一个 API 网关,其 POST 请求触发了上述 lambda function。 在此处输入图像描述

Then, I used the REST API gateway to send a POST request with the below body content with a header Content-Type: application/json .然后,我使用 REST API 网关发送带有以下正文内容的 POST 请求,其中包含 header Content-Type: application/json

request body:请求正文:

{
  "job_title": "software engineer"
}

But it is navigating to the else block in the lambda code (indicating that API gateway is not properly intercepting the POST request parameters) which is not expected.但它正在导航到 lambda 代码中的else块(表明 API 网关未正确拦截 POST 请求参数),这是意料之外的。

I tried permutations and combinations with the API gateway configurations in AWS.我尝试使用 AWS 中的 API 网关配置进行排列和组合。 But none of them worked.但他们都没有工作。 I get a feel that my lambda function might need to be changed to make it work with API gateway.我觉得我的 lambda function 可能需要更改以使其与 API 网关一起使用。 But none of my attempts succeeded so far.但到目前为止,我的任何尝试都没有成功。 Any suggestions would be highly appreciated.任何建议将不胜感激。 Thanks!谢谢!

By default, the POST request is not sent directly to the Lambda as the event object.默认情况下,POST 请求不会作为event object 直接发送到 Lambda。 The event object will have much more in it than what you have in your test case, and the actual POST data will be down in one of the event object's properties. event object 将比您在测试用例中拥有的要多得多,并且实际的 POST 数据将在事件对象的属性之一中下降。 If you are using Lambda proxy integration in API Gateway, then the event will look like this .如果您在 API 网关中使用 Lambda 代理集成,则事件将如下所示

You have two options:你有两个选择:

  1. Create a custom mapping template in API Gateway to convert the event sent to Lambda into what you are expecting.在 API 网关中创建自定义映射模板,以将发送到 Lambda 的事件转换为您所期望的。

  2. Modify your Lambda code to handle the format that is currently being sent.修改您的 Lambda 代码以处理当前正在发送的格式。 I suggest using this library .我建议使用这个库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM