javascript - Provide multiple path parameters to API Gateway (serverless) - Stack Overflow

I have a method "DB_Update" in a module.This method requires several Parameters as Input (In

I have a method "DB_Update" in a module.

This method requires several Parameters as Input (InputA, InputB and InputC)

module.exports.DB_Update = async (event) => 
{

    //extract Parameters from event
    InputA= event.pathParameters.InputA
    InputB= event.pathParameters.InputB
    InputC= event.pathParameters.InputC
   
    // Update Items in DB based on Input
    //...
}

I would like to invoke the function via an API request using serverless and AWS API Gateway

Hence in my serverless yml file I have added the function


DB_Update:
    handler: ../DB_Update
    events:
      - http:
          path: DB_Update/{InputA, InputB, InputB}
          method: get

and finally I invoke the endpoint via Postman using the parameters

http://localhost:3000/dev/DB_Update/InputA=9783404163809&InputB=111&InputC=BB

However regardless of which alternation I try I don't get it to work. Either yml does not accept the bination of Input Parameters or I dont get an event object back.

Hence it would be great if you could give me a hint how to make this work. Thanks!

I have a method "DB_Update" in a module.

This method requires several Parameters as Input (InputA, InputB and InputC)

module.exports.DB_Update = async (event) => 
{

    //extract Parameters from event
    InputA= event.pathParameters.InputA
    InputB= event.pathParameters.InputB
    InputC= event.pathParameters.InputC
   
    // Update Items in DB based on Input
    //...
}

I would like to invoke the function via an API request using serverless and AWS API Gateway

Hence in my serverless yml file I have added the function


DB_Update:
    handler: ../DB_Update
    events:
      - http:
          path: DB_Update/{InputA, InputB, InputB}
          method: get

and finally I invoke the endpoint via Postman using the parameters

http://localhost:3000/dev/DB_Update/InputA=9783404163809&InputB=111&InputC=BB

However regardless of which alternation I try I don't get it to work. Either yml does not accept the bination of Input Parameters or I dont get an event object back.

Hence it would be great if you could give me a hint how to make this work. Thanks!

Share Improve this question asked Jun 21, 2020 at 9:17 GeoleGeole 3761 gold badge5 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You need to decide if you want to pass the parameters as Path Parameters (e.g. baseurl/{varA}/{varB}/{varC}) or Query Parameters (e.g. baseurl?varA=x&varB=y&varC=z). This answer provides some more insight into the different patterns.

Depending on which pattern you decide, request parameters should be included in the serverless.yml file in the following format (set fields to true if they're required, false if optional):

Path Parameters

DB_Update:
    handler: ../DB_Update
    events:
      - http:
          path: DB_Update/{InputA}/{InputB}/{InputC}
          method: get
          request:
            parameters:
              paths:
                InputA: true
                InputB: true
                InputC: true

Query Parameters:

DB_Update:
    handler: ../DB_Update
    events:
      - http:
          path: DB_Update
          method: get
          request:
            parameters:
              querystrings:
                InputA: true
                InputB: true
                InputC: true

Visit this section of the Serverless Framework documentation for more information.

Answer above answers great. Just one note that you actually don't have to specify parameters under request for path parameters. Something like that is enough:

DB_Update:
    handler: ../DB_Update
    events:
      - http:
          path: DB_Update/{InputA}/{InputB}/{InputC}
          method: get

Also you can mix path and query parameters, so you can have something like this:

    handler: ../DB_Update
    events:
      - http:
          path: DB_Update/{InputD}
          method: get
          request:
            parameters:
              querystrings:
                InputA: true
                InputB: true
                InputC: true

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744842240a4596627.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信