how to get metadata in grpc impl

43 views
Skip to first unread message

Ashish Srivastava

unread,
Sep 5, 2021, 4:08:15 PM9/5/21
to grpc.io
Hi Team,
i am very new in GRPC. I have created a service and i want to get the header in my service. I have added the server intercepter but not able to get my header in below service.

Can any one help ? 

@Override
public void sendPing(Ping pingRequest, StreamObserver<Pong> pongResponseObserver){
Pong pong= null;
try {
pong = createPongResponse(pingRequest);
} catch (ServiceException e) {
logger.log(Level.WARNING,"Error in PingPong Service");
Metadata metadata = trailersFromThrowable(e);
pongResponseObserver.onError(FAILED_PRECONDITION.asRuntimeException(metadata));
}
pongResponseObserver.onNext(pong);
pongResponseObserver.onCompleted();
}


in metadata i am passing below json and in debugging mode it's visible in interceptor
{
    "userId":"srivastava"
}

Interceptor code:

public class HeaderServerInterceptor implements ServerInterceptor {

private static final Logger logger = Logger.getLogger(HeaderServerInterceptor.class.getName());

@VisibleForTesting
static final Metadata.Key<String> CUSTOM_HEADER_KEY =
Metadata.Key.of("custom_server_header_key", Metadata.ASCII_STRING_MARSHALLER);

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
final Metadata requestHeaders,
ServerCallHandler<ReqT, RespT> next) {
logger.log(Level.WARNING,"header received from client:" + requestHeaders);
return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendHeaders(Metadata responseHeaders) {
responseHeaders.put(CUSTOM_HEADER_KEY, "customRespondValue");
super.sendHeaders(responseHeaders);
}
}, requestHeaders);
}
}



Eric Anderson

unread,
Sep 8, 2021, 2:46:44 PM9/8/21
to Ashish Srivastava, grpc.io
I responded to your question on Gitter. I said:

I don't think I understand. You are wanting to get CUSTOM_HEADER_KEY's "customRespondValue" within sendPing()? 

It is really weird that the service would want to read response headers. So I didn't think I understood your objective.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/0378f968-49b8-4122-806b-6f449097f26an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages