Android How to add a custom header in grpc client?
Asked Answered
H

1

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.

Homopolar answered 16/7, 2017 at 6:10 Comment(1)
One can use the predefined interceptor with MetadataUtils.newAttachHeadersInterceptor(metadata)Renascence
H
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)) 
Homopolar answered 18/7, 2017 at 17:25 Comment(6)
so we dont need interceptor for attaching header as you mentioned right ?Disunity
Both should work. adding interceptor or as the metadataHomopolar
attachHeaders is now deprecated. Use this: stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(header))Mahound
@Mahound Are you sure? I use the code MetadataUtils.attachHeaders and it's not showing that deprecated.Alluring
@Heisenberg which version are you using? check the docs grpc.github.io/grpc-java/javadoc/io/grpc/stub/…Mahound
@Heisenberg This was long ago lol in one of my previous projectHomopolar

© 2022 - 2024 — McMap. All rights reserved.