简体   繁体   English

将POST请求正文通过Amazon API Gateway传递给Lambda

[英]passing POST request body through Amazon API Gateway to Lambda

I have a AWS Lambda function written in Java, that gets triggered by an AWS API Gateway call. 我有一个用Java编写的AWS Lambda函数,它由AWS API Gateway调用触发。

I am trying to make a POST request to one of the endpoints with a JSON as payload. 我正在尝试使用JSON作为有效负载向其中一个端点发出POST请求。

curl -H "Content-Type: application/json" -X POST -d '{"firstName":"Mr", "lastName":"Awesome"}' https://someexample.execute-api.eu-central-1.amazonaws.com/beta/MethodHandlerLambda

The gateway will then detect the Content-Type and pass all request parameters (including the body) through a default template . 然后,网关将检测 Content-Type并通过默认模板传递所有请求参数(包括正文)。 The interesting part is this one 有趣的是这一部分

#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
 ....

It is supposed to present me with a Map<String, Object> that is passed to my Java method: 它应该向我提供一个传递给我的Java方法的Map<String, Object>

public void myHandler(Map<String, Object> input, Context context){
    input.keySet().forEach((key) -> {
        System.out.println(key + " : " + input.get(key));
    });
}

And the result should be something like this: 结果应该是这样的:

body-json : {"firstName":"Mr", "lastName":"Awesome"}
...

But what I am getting is this: 但我得到的是这样的:

body-json : {firstName=Mr, lastName=Awesome}

Another possibility would be to pass the whole body as string: 另一种可能性是将整个身体作为字符串传递:

"body" : $input.body

but that again just "converts" to key=value instead of key:value 但这又是“转换”为key=value而不是key:value

How do I have to configure the template to simply pass me the body so I can use it in a JSON parser? 我如何配置模板只是简单地传递给我身体,以便我可以在JSON解析器中使用它?

And again - just posting a question here on SO helps to find the answer on your own :) 再一次 - 只需在SO上发布一个问题就可以自己找到答案:)

In the AWS Api Gateway template I set the body to 在AWS Api Gateway模板中,我将主体设置为

"body-json" : $input.body

which should return the complete payload as a String. 应该将完整的有效负载作为String返回。

But more importantly I read Greggs answer to his own question and changed my method to 但更重要的是,我读了格雷格斯回答他自己的问题,并改变了我的方法

public void myHandler(InputStream inputStream, OutputStream outputStream, Context context) throws IOException{
    final ObjectMapper objectMapper = new ObjectMapper();
    JsonNode json = objectMapper.readTree(inputStream);
    System.out.println(json.toString());        
}

So, it is sufficient to have a simple InputStream and read it as a JsonNode with whatever JSON library one prefers (I am using Jackson FasterXML). 因此,只需一个简单的InputStream并将其作为JsonNode读取就可以使用任何更喜欢的JSON库(我使用的是Jackson FasterXML)。 And voila, it packs all possible parameters in a single JSON (as specified by the template) 瞧,它将所有可能的参数打包在一个JSON中(由模板指定)

{
    "body-json": {
        "firstName": "Mr",
        "lastName": "Awesome"
    },
    "params": {
       ...
    },
    "stage-variables": {
       ...
    },
    "context": {
        ...
    }
}

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

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