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

Post requests do not support binding both body and query #2907

Closed
hoslo opened this issue Jul 10, 2023 · 8 comments · Fixed by #2915
Closed

Post requests do not support binding both body and query #2907

hoslo opened this issue Jul 10, 2023 · 8 comments · Fixed by #2915
Labels
bug Something isn't working

Comments

@hoslo
Copy link
Contributor

hoslo commented Jul 10, 2023

What happened:

The Post request did not get the query parameter

What you expected to happen:

Get both body and query

How to reproduce it (as minimally and precisely as possible):

Post requests have both query and body

Anything else we need to know?:

Environment:

  • Kratos version (use kratos -v): v2.5.3
  • Go version (use go version): go version go1.20.5 darwin/arm64
  • OS (e.g: cat /etc/os-release): macos
  • Others:
@hoslo hoslo added the bug Something isn't working label Jul 10, 2023
@tonyhal
Copy link

tonyhal commented Jul 13, 2023

当前有解决方案吗?

@kratos-ci-bot
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Is there a current solution?

@tonyhal
Copy link

tonyhal commented Jul 14, 2023

有没有其他解决方案,比较着急!

@kratos-ci-bot
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Is there any other solution, more anxious!

@kingofzihua
Copy link

kingofzihua commented Jul 14, 2023

http.Context 里面是有 BindBindQueryBindVars 这个三个方法的,只不过protobuf 生成的内容是没有的,建议逐层替换

以下只是一个例子
在生成的 xxx.pb.go 文件同级目录创建一个新的go文件,重写掉 _UserService_CreateUser0_HTTP_Handler 方法,自己手动绑定

image

然后重写 RegisterUserServiceHTTPServer 方法的路由注册方法 ,指向自己重写掉的 handler

@tonyhal
Copy link

tonyhal commented Jul 14, 2023

目前手动改模板是可以的,但是使用 make api 模板就会变更 比较麻烦

@kratos-ci-bot
Copy link
Collaborator

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


when to change the template

@czyt
Copy link
Contributor

czyt commented Jul 15, 2023

service define

rpc CreateUser(CreateUserRequest) returns (CreateUserResponse) {
    option (google.api.http) = {
      post: "/users",
      body: "user"
    };
  }
  
message CreateUserRequest {
  string password = 1;
  User user = 2;
}
message CreateUserResponse {
  string response = 1;
}

message User {
  string first_name = 1;
  string last_name = 2;
}
  

in your service code

func (s *GreeterService) CreateUser(ctx context.Context, req *v1.CreateUserRequest) (*v1.CreateUserResponse, error) {
	info := fmt.Sprintf("password:%s,userName:%s %s", req.Password, req.User.FirstName, req.User.LastName)
	return &v1.CreateUserResponse{Response: info}, nil
}

test with curl

curl --location -g --request POST 'http://127.0.0.1:8000/users?password=e77eEDab-BdAe-78BE-0979-2F798d9bBe4b \
--header 'Content-Type: application/json' \
--data-raw '{
    "first_name":"czyt",
    "last_name":"cn"
}'

the curl response

{
    "response": "password:e77eEDab-BdAe-78BE-0979-2F798d9bBe4b,userName:czyt cn"
}

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.

5 participants