0
votes

I am not using CallCredentials as of now with GRPC-Java. I need to pass in multiple fields in the headers for certain calls. I have populated the headers as given below and added an interceptor which gets called upon while using an async Stub/channel.

populate header

public Metadata generateHeader() {
    Metadata.Key<String> jwtKey = Metadata.Key.of("jwt", Metadata.ASCII_STRING_MARSHALLER);
    headers.put(jwtKey, jwt);
    Metadata.Key<String> testKey = Metadata.Key.of("testID", Metadata.ASCII_STRING_MARSHALLER);
    headers.put(testKey, testID);
    return headers;
} 

I am accessing the headers field as an instance variable passed into the client interceptor. I can see that the headers contains the intended value till it gets into the start call at which point it becomes null (I obviously do not see the headers populated on the server side). Any ideas to fix this (or should i be using CallCredentials). Documentation for GRPC-Java needs a lot of work.

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
        MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions,
        Channel next) {
    log.info("HEADER INTERCEPTOR{}", headers.get(Metadata.Key.of("jwt", Metadata.ASCII_STRING_MARSHALLER)));
    ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
    return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(call) {
        @Override
        public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
            log.info("HEADER START {}", headers.keys());
            super.start(responseListener, headers);
        }
    };
}  
1

1 Answers

-1
votes

I had to access the headers via a method call after which I was able to get the header keys/values.

Metadata cached = getHeaders().get(Metadata.Key.of("testID", Metadata.ASCII_STRING_MARSHALLER)));
headers.merge(cached);

&

   public Metadata getHeaders() {
        return headers;
}