我们可以在大猩猩子路道路径上加入vars吗?

发布于 2025-01-28 16:48:51 字数 611 浏览 4 评论 0原文

我正在尝试将子路线添加到我的路由器代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2025-02-04 16:48:51

好的,所以昨晚我在梦中找到了一个解决方案哈哈,

基本上是以下前缀的问题:

managementRouter := baseRouter.PathPrefix("/managing/{id}").Subrouter()

路由器无法知道id> id字段停止的位置。因此,当我们使用此URL访问端点时:/api/v1/takining/627E6F7E05DB3552970E1164/add-employees ,路由器认为{ID} 627E6F7E05DB3552970E1164/add-employees,并且在其之后不匹配任何路线。

因此,我发现的解决方案是告诉路由器变量之后发生了什么。为此,您只需在变量之后添加斜线:

managementRouter := baseRouter.PathPrefix("/managing/{id}/").Subrouter()

Ok so I found a solution in my dream last night haha

Basically the problem with the following prefix :

managementRouter := baseRouter.PathPrefix("/managing/{id}").Subrouter()

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 literally 627e6f7e05db3552970e1164/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 :

managementRouter := baseRouter.PathPrefix("/managing/{id}/").Subrouter()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文