[英]serverless-offline optional path parameter
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.我正在尝试使用可选参数设置 GET 请求,但是当我在没有可选参数的情况下在本地调用 url 时出现错误。 It works fine online on lambda though.不过,它在 lambda 上在线运行良好。 What did I do wrong?我做错了什么?
I'm using serverless version 1.24.1 with the serverless-offline plugin version 3.16.0我正在使用无服务器版本 1.24.1 和无服务器离线插件版本 3.16.0
here is my request definition in serverless.yml :这是我在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:这个 url 工作:
http://localhost:3000/item/store/123456/used
this don't这不
http://localhost:3000/item/store/123456
and gives me this output给我这个 output
{
statusCode: 404,
error: "Serverless-offline: route not found.",
currentRoute: "get - /item/store/123456",
existingRoutes: [
"get - item/store/{storeid}/{itemstatus}"
]
}
Thanks a lot非常感谢
Add "?" 添加“?” after the params can make it work. 在params之后可以使它工作。
functions:
getitems:
handler: lambda.handler
events:
- http:
path: item/store/{storeid}/{itemstatus?}
method: get
cors: true
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. 发生错误:ApiGatewayResourceExperimentExperimentVarPsizeVar - 资源的路径部分仅允许a-zA-Z0-9 ._-和开头和结尾的花括号。
The current workaround to this is adding http handlers for each 'optional' variable in the path like so: 目前的解决方法是为路径中的每个“可选”变量添加http处理程序,如下所示:
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
If you want itemstatus
to be optional then you must set it to false in your serverless request definition like so: 如果您希望itemstatus
是可选的,那么您必须在无服务器请求定义中将其设置为false,如下所示:
- http:
path: item/store/{storeid}/{itemstaus}
method: get
cors: true
request:
parameter:
storeid: true
itemstatus: false
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.