Android How to add a custom header in grpc client?

rana picture rana · Jul 16, 2017 · Viewed 11k times · Source

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.

Answer

rana picture rana · Jul 18, 2017

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 = MetadataUtils.attachHeaders(stub, header);