reference to MetaData object in grpc service method

52 views
Skip to first unread message

Mahesh Kanote

unread,
Mar 31, 2018, 7:15:21 AM3/31/18
to grpc.io
Hi,

I have question about getting MetaData in grpc service method. By Implementing io.grpc.ServerInterceptor its possible to get reference of MetaData sent by client. But, is there any way we can get reference to MetaData object in grpc service method? Something like HttpServletRequest and HttpServletResponse in case of RestControllers.

Thanks,
Mahesh

Carl Mastrangelo

unread,
Apr 3, 2018, 12:42:16 PM4/3/18
to grpc.io
gRPC Java does not expose all the headers, because some of them are gRPC specific.  What headers did you want to access?

Mahesh Kanote

unread,
Apr 3, 2018, 12:51:13 PM4/3/18
to Carl Mastrangelo, grpc.io
I wanted to access custom headers sent by client as meta data. I am able to do it using context object in server interceptor. 

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/92935121-afc4-4e5f-beda-e945b75c78c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carl Mastrangelo

unread,
Apr 5, 2018, 1:28:08 PM4/5/18
to grpc.io
Can you give a snippet of code showing what you are trying to do but can't?

Mahesh Kanote

unread,
Apr 6, 2018, 2:09:23 AM4/6/18
to Carl Mastrangelo, grpc.io
I am able to do it now with below snippet. 

Interceptor:
public class ContextInterceptor implements ServerInterceptor {
  @Override
  public <ReqT, RespT> Listener<ReqT> interceptCall(
      ServerCall<ReqT, RespT> call,
      Metadata headers,
      ServerCallHandler<ReqT, RespT> next) {
    String id = headers.get(METADATA_KEY);
    Context ctx = Context.current().withValue(CONTEXT_KEY, id);

    return Contexts.interceptCall(ctx, call, headers, next);
  }
}

In gRpc service:
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
    HelloReply reply = HelloReply.newBuilder()
        .setMessage("name = " + request.getName() + ", id = " + Keys.CONTEXT_KEY.get())
        .build();
    responseObserver.onNext(reply);
    responseObserver.onCompleted();
  }


Thanks,
Mahesh

To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

For more options, visit https://groups.google.com/d/optout.



--


Thanks,
Mahesh
Reply all
Reply to author
Forward
0 new messages