You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
kratos-ci-bot
changed the title
生成URL时,没有正确替换路径参数{xxx},而是错误地将xxx作为查询参数添加
When generating the URL, the path parameter {xxx} is not replaced correctly, but instead xxx is added as a query parameter incorrectly
Mar 11, 2025
Hey there, @tangqiu0205! I'm here to help you with any bugs, questions, or contributions. Let's tackle this together while we wait for a human maintainer.
To correctly replace the path parameter {workspace_id} in the URL using Kratos v2.8.4 and Go v1.20, ensure that the EncodeURL function is used properly. Here's a concise example of how to achieve this:
package main
import (
"fmt""github.com/go-kratos/kratos/v2/transport/http/binding"
)
typeMyMessagestruct {
WorkspaceIDstring`form:"workspace_id"`
}
funcmain() {
pathTemplate:="/wayline/api/v1/workspaces/{workspace_id}/tasks"msg:=&MyMessage{WorkspaceID: "your-uuid-value"}
// Call EncodeURL with needQuery set to false to replace path parametersurl:=binding.EncodeURL(pathTemplate, msg, false)
fmt.Println(url) // Output: /wayline/api/v1/workspaces/your-uuid-value/tasks
}
In this example, the MyMessage struct has a field WorkspaceID tagged with form:"workspace_id", which matches the path parameter {workspace_id} in the pathTemplate. The EncodeURL function will replace {workspace_id} with the value from msg.WorkspaceID, resulting in the correct URL without adding workspaceId as a query parameter [1].
What happened:
生成URL时,没有正确替换路径参数{workspace_id},而是错误地将workspaceId作为查询参数添加
What you expected to happen:
正确替换
How to reproduce it (as minimally and precisely as possible):
使用kratos v2.8.4
Anything else we need to know?:
Environment:
kratos -v
): v2.8.4go version
): v1.20cat /etc/os-release
): wslThe text was updated successfully, but these errors were encountered: