Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When generating the URL, the path parameter {xxx} is not replaced correctly, but instead xxx is added as a query parameter incorrectly #3560

Open
tangqiu0205 opened this issue Mar 11, 2025 · 1 comment · May be fixed by #3311
Labels
bug Something isn't working

Comments

@tangqiu0205
Copy link

What happened:

生成URL时,没有正确替换路径参数{workspace_id},而是错误地将workspaceId作为查询参数添加

Image

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 version (use kratos -v): v2.8.4
  • Go version (use go version): v1.20
  • OS (e.g: cat /etc/os-release): wsl
  • Others:
@tangqiu0205 tangqiu0205 added the bug Something isn't working label Mar 11, 2025
@kratos-ci-bot 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
Copy link

dosubot bot commented 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"
)

type MyMessage struct {
	WorkspaceID string `form:"workspace_id"`
}

func main() {
	pathTemplate := "/wayline/api/v1/workspaces/{workspace_id}/tasks"
	msg := &MyMessage{WorkspaceID: "your-uuid-value"}

	// Call EncodeURL with needQuery set to false to replace path parameters
	url := 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].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant