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
ServerInterceptor how to get header/trailer data #4317
Comments
This can be achieved by replacing the ServerTransportStreamFromContext in the context with a wrapped one, and override type wrapper struct {
grpc.ServerTransportStream
}
stream := grpc.ServerTransportStreamFromContext(ctx)
wrappedStream = &wrapper{stream}
newCtx := grpc.NewContextWithServerTransportStream(ctx, wrappedStream) |
@menghanl you mean this?
my problem is how can i override |
You are not implementing type wrapper struct {
grpc.ServerTransportStream
header, trailer metadata.MD
}
func (w *wrapper) Method() string {
}
func (w *wrapper) SetHeader(md metadata.MD) error{
if err := w.ServerTransportStream.SetHeader(md); err != nil {
return err
}
w.header = md
return nil
}
func (w *wrapper) SendHeader(md metadata.MD) error{
...
}
func (w *wrapper) SetTrailer(md metadata.MD) error{
...
}
stream := grpc.ServerTransportStreamFromContext(ctx)
wrappedStream = &wrapper{stream}
newCtx := grpc.NewContextWithServerTransportStream(ctx, wrappedStream)
hander(newCtx, ...)
fmt.Println(wrappedStream.header) |
Thank you for your reply, this indeed solve my problem, now this issue can be closed. At last, I really hope you can think to expose more APIs in such as And maybe this is also a common scene for other users. |
I agree that ideally there should be an easier way to do this, but that will need a closer look. The Or, we can always wrap as mentioned above, so that it's available to all user interceptors. |
scene
I use
grpc.UnaryServerInterceptor
/grpc.StreamServerInterceptor
to do some common logic before/after the biz code.in the common logic, i want to get request header before biz code and get response header/trailer after biz code, but i found i can't do the latter.
my implementation
question
I can't find an easy way to get header/trailer after the biz logic, so i use reflect.
In my opinion, get header/trailer after the biz logic maybe a feature that many people need, so did i lost some doc about this or this feature is indeed unavailable now, need a PR? :)
looking forward reply.
The text was updated successfully, but these errors were encountered: