Geole
Geole

Reputation: 376

Provide multiple path parameters to API Gateway (serverless)

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 combination 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!

Upvotes: 0

Views: 6778

Answers (2)

karjan
karjan

Reputation: 1016

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

Upvotes: 1

j-petty
j-petty

Reputation: 3026

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.

Upvotes: 4

Related Questions