最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论