25

I have to add a custom header in an android grpc client. I am unable to send it successfully.

public class HeaderClientInterceptor implements ClientInterceptor {
    @Override
    public < ReqT, RespT > ClientCall < ReqT, RespT > interceptCall(MethodDescriptor < ReqT, RespT > method,
        CallOptions callOptions, Channel next) {

        return new SimpleForwardingClientCall < ReqT, RespT > (next.newCall(method, callOptions)) {

            @Override
            public void start(Listener < RespT > responseListener, Metadata headers) {
                /* put custom header */
                Timber.d("header sending to server:");


                Metadata fixedHeaders = new Metadata();
                Metadata.Key < String > key =
                    Metadata.Key.of("Grps-Matches-Key", Metadata.ASCII_STRING_MARSHALLER);
                fixedHeaders.put(key, "primary.secondary");

                headers.merge(fixedHeaders);

                super.start(new SimpleForwardingClientCallListener < RespT > (responseListener) {
                    @Override
                    public void onHeaders(Metadata headers) {
                        /**
                         * if you don't need receive header from server,
                         * you can use {@link io.grpc.stub.MetadataUtils attachHeaders}
                         * directly to send header
                         */

                        Timber.e("header received from server:" + headers.toString());
                        super.onHeaders(headers);
                    }
                }, headers);
            }
        };
    }
}

EDIT: Added the custom header using this way successfully

Now in my grpc call, I am calling like this

ClientInterceptor interceptor = new HeaderClientInterceptor();
Channel channel = ManagedChannelBuilder.forAddress(BuildConfig.HOST, BuildConfig.PORT).build();
Channel channelWithHeader = ClientInterceptors.intercept(channel, interceptor);
ServiceGrpc.ServiceBlockingStub service = ServiceGrpc.newBlockingStub(channelWithHeader);

I have built the above request and calling it in the pseudo call as below.

Iterator<Model> dataItems = service.getItems(SOMERequestBuilderObj);

I am trying to add a custom header like this "Grps-Matches-Key : primary.secondary"

In Rest API call I would have added this as a header like

builder.header("Grps-Matches-Key", "primary.secondary");

Hope this helps.

OhhhThatVarun
  • 3,981
  • 2
  • 26
  • 49
rana
  • 1,824
  • 3
  • 21
  • 36
  • 2
    One can use the predefined interceptor with `MetadataUtils.newAttachHeadersInterceptor(metadata)` – kza Jul 09 '19 at 11:29

1 Answers1

46

The edited version in the question works too.In GRPC there are many ways to add headers (called meta data) . We can add meta data like in my question above using interceptor or we can add meta data for the client stub or you can add it before making request in the client stub channel .

// create a custom header
Metadata header=new Metadata();
Metadata.Key<String> key =
    Metadata.Key.of("Grps-Matches-Key", Metadata.ASCII_STRING_MARSHALLER);
header.put(key, "match.items");

// create client stub
ServiceGrpc.ServiceBlockingStub stub = ServiceGrpc
    .newBlockingStub(channel);

Add the header before making any request as shown here using MetadataUtils

stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(header)) 
rana
  • 1,824
  • 3
  • 21
  • 36
  • so we dont need interceptor for attaching header as you mentioned right ? – Tarun Rawat Mar 23 '20 at 12:34
  • 2
    Both should work. adding interceptor or as the metadata – rana Apr 06 '20 at 22:17
  • attachHeaders is now deprecated. Use this: stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(header)) – ozma Feb 08 '22 at 10:32
  • @ozma Are you sure? I use the code `MetadataUtils.attachHeaders` and it's not showing that deprecated. – Arefe Mar 02 '22 at 14:47
  • @Heisenberg which version are you using? check the docs https://grpc.github.io/grpc-java/javadoc/io/grpc/stub/MetadataUtils.html#attachHeaders-T-io.grpc.Metadata- – ozma Mar 02 '22 at 15:46
  • @Heisenberg This was long ago lol in one of my previous project – rana Mar 03 '22 at 01:11