7

I'm trying to set up a GET request with an optional parameter but I get an error when I call the url locally without the optional parameter. It works fine online on lambda though. What did I do wrong?

I'm using serverless version 1.24.1 with the serverless-offline plugin version 3.16.0

here is my request definition in serverless.yml:

functions:
getitems:
    handler: lambda.handler
    events:
      - http:
            path: item/store/{storeid}/{itemstatus}
            method: get
            cors: true
            request:
                parameters:
                  paths:
                    storeid: true
                    itemstatus: false

this url works:

http://localhost:3000/item/store/123456/used

this don't

http://localhost:3000/item/store/123456

and gives me this output

{
   statusCode: 404,
   error: "Serverless-offline: route not found.",
   currentRoute: "get - /item/store/123456",
   existingRoutes: [
       "get - item/store/{storeid}/{itemstatus}"
   ]
}

Thanks a lot

Tom Ines
  • 103
  • 1
  • 7

4 Answers4

5

Unfortunately Chen Dachao's answer fails with:

An error occurred: ApiGatewayResourceExperimentExperimentVarPsizeVar - Resource's path part only allow a-zA-Z0-9._- and curly braces at the beginning and the end.

The current workaround to this is adding http handlers for each 'optional' variable in the path like so:

functions:
  getitems:
    handler: lambda.handler
      events:
        - http:
            path: item/store/{storeid}
            method: get
            cors: true
            request:
              parameter:
                storeid: true
        - http:
            path: item/store/{storeid}/{itemstaus}
            method: get
            cors: true
            request:
              parameter:
                storeid: true
                itemstatus: true
CoderX
  • 162
  • 1
  • 9
1

Add "?" after the params can make it work.

functions:
  getitems:
  handler: lambda.handler
  events:
    - http:
        path: item/store/{storeid}/{itemstatus?}
        method: get
        cors: true
Chen Dachao
  • 1,736
  • 2
  • 21
  • 36
0

If you want itemstatus to be optional then you must set it to false in your serverless request definition like so:

- http:
            path: item/store/{storeid}/{itemstaus}
            method: get
            cors: true
            request:
              parameter:
                storeid: true
                itemstatus: false
  • Error on deployment: Resource's path part only allow a-zA-Z0-9._- and curly braces at the beginning and the end. (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException – Sielu Aug 23 '19 at 14:34
0

I've used the following option and it worked with and without the parameters

- http:
      path: auth/{role?}
      method: get
      request:
          parameter:
              paths:
                  role: false