我们可以在大猩猩子路道路径上加入vars吗?
我正在尝试将子路线添加到我的路由器代码:
router := mux.NewRouter()
baseRouter := router.PathPrefix("/api/v1").Subrouter()
managementRouter := baseRouter.PathPrefix("/managing/{id}").Subrouter()
managementRouter.Use(auth.ManagingMiddleware)
managementRouter.HandleFunc("/add-employees", management.AddEmployeesToOrganization).Methods("POST")
目标是强迫客户在每个调用上给出id
变量ManagementRouter
功能。 虽然,当我发送这样的请求时:
/api/v1/managing/627e6f7e05db3552970e1164/add-employees
...我得到404。我错过了什么还是不可能?
I'm trying to add a subrouter to my router code :
router := mux.NewRouter()
baseRouter := router.PathPrefix("/api/v1").Subrouter()
managementRouter := baseRouter.PathPrefix("/managing/{id}").Subrouter()
managementRouter.Use(auth.ManagingMiddleware)
managementRouter.HandleFunc("/add-employees", management.AddEmployeesToOrganization).Methods("POST")
The goal is to force the client to give an id
variable on each call to managementRouter
functions.
Although, when i send a request like this :
/api/v1/managing/627e6f7e05db3552970e1164/add-employees
... I get a 404. Am I missing something or is it just not possible ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以昨晚我在梦中找到了一个解决方案哈哈,
基本上是以下前缀的问题:
路由器无法知道
id> id
字段停止的位置。因此,当我们使用此URL访问端点时:/api/v1/takining/627E6F7E05DB3552970E1164/add-employees
,路由器认为{ID}
,并且在其之后不匹配任何路线。627E6F7E05DB3552970E1164/add-employees
因此,我发现的解决方案是告诉路由器变量之后发生了什么。为此,您只需在变量之后添加斜线:
Ok so I found a solution in my dream last night haha
Basically the problem with the following prefix :
Is that the router has no way of knowing where the
id
field stops. So when we access an endpoint with for example this url :/api/v1/managing/627e6f7e05db3552970e1164/add-employees
, the router believes that the{id}
variable is literally627e6f7e05db3552970e1164/add-employees
and doesn't match any route after it.So the solution I found is to tell the router what comes after the variable. For that, you just add a slash after the variable :