简体   繁体   中英

How to prepare a request body to test AWS lambda function which accepts an url parameter AND request body as input?

so basically I'm trying to create an AWS lambda function which has to accept an URL parameter and also a request body as input parameter. I know how to create this function, but I don't know how to create a Test request on AWS. So my question here is what is the correct form of a test request for a function which acceps an URL parameter and a request body on AWS lambda "Configure test event"? And later how to configure this in API gateway.



This is how I test my function via postman and it works perfectly: 在此处输入图片说明

You can see in the picture that one parameter gets passed via the url ("95") and request body is also sent to the endpoint (the json in the body). How can I do the same request in the "Configure test event" option on aws amazon, because there you can only specify json as you can see in the picture below, there is no option to specify an url parameter



For example: for a function which accepts only a URL parameter, a test request can look like this: 在此处输入图片说明

and a test request for a function which accepts only request body looks like this:

在此处输入图片说明



I've tried a lot of combinations but I can't get it right, as I have no idea how to combine the URL parameter and the request body together...This doesn't work在此处输入图片说明



And this is my function (method) code:

        public int updateTariffPoint(int id_tariffpoints, List<JsonTPModel> tariffPoints){
            return 42;
        }

Let's go step by step and separate issues you have.

  1. Test events for Lambda you mentioned is enough to test Lambda itself.
    JSON parameters you specify there is exactly how Lambda will receive them. Please note that Lambda works with JSON format of inputs (ie if you want to send multipart request, you will need to convert it to JSON based in Gateway.).

  2. Signature of updateTariffPoint function doesn't correspond to Lambda function signature and it won't be executed by AWS.
    Details and examples of proper signature are here . You can use Map<String, Object> as inputType in your function to directly access all JSON parameters.

  3. Configuration of API Gateway is straightforward for your case.
    For example check this guide . Your case will be simpler since you use plain JSON.

As example for Java Lambda you can use this one . It handles more complex case but you can see all sources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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