gin - 访问包含正斜杠的 url 编码路径参数时出现问题
对于带有路径参数的给定路由(下面的示例)
router.GET("/employee/:id", empHandler.GetEmployee)
当尝试调用包含正斜杠的 id 路径参数(编码)的 url 时
id=21/admin/527
网址编码 ID = 21%2Fadmin%2F527
https://localhost:8000/emplayee/21%2Fadmin%2F527
当我尝试满足此请求时收到 404 看来 gin 会自动解码路径参数并形成一个包含解码路径参数的 url 的路由
https://localhost:8000/emplayee/21/admin/527
我想要员工 ID 路径参数的确切编码值,因为它用于调用其他 api,这需要它是 url-编码。
For a given route with path param (example below)
router.GET("/employee/:id", empHandler.GetEmployee)
When tried to invoke the url with id path-param(encoded) containing forward slashes
id = 21/admin/527
url-encoded id = 21%2Fadmin%2F527
https://localhost:8000/emplayee/21%2Fadmin%2F527
I'm getting 404 when I try to hit this request
It seems that gin is automatically decoding the path param and forming a route with url containing decoded path-param
https://localhost:8000/emplayee/21/admin/527
I want the exact encoded value for employee id path-param since it is to be used for calling other api which requires it to be url-encoded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过使用以下选项配置路由器解决了这个问题,
这解决了 404 错误,并且 gin 上下文返回相同的编码(未转义)值。
该值现在可用于调用其他需要员工 ID 的 url 编码(未转义)值的 API
I've resolved this issue by configuring the router with below options
This resolved the 404 error, also gin context return the same encoded(unescaped) value.
This value can now be used to call the other APIs which requires url-encoded(unescaped) value for employee id