title: Android How to add a custom header in grpc client?
body: <p>I have to add a custom header in an android grpc client. I am unable to send it successfully.</p>
  
  <pre><code>public class HeaderClientInterceptor implements ClientInterceptor {
      @Override
      public &lt; ReqT, RespT &gt; ClientCall &lt; ReqT, RespT &gt; interceptCall(MethodDescriptor &lt; ReqT, RespT &gt; method,
          CallOptions callOptions, Channel next) {
  
          return new SimpleForwardingClientCall &lt; ReqT, RespT &gt; (next.newCall(method, callOptions)) {
  
              @Override
              public void start(Listener &lt; RespT &gt; responseListener, Metadata headers) {
                  /* put custom header */
                  Timber.d("header sending to server:");
  
  
                  Metadata fixedHeaders = new Metadata();
                  Metadata.Key &lt; String &gt; key =
                      Metadata.Key.of("Grps-Matches-Key", Metadata.ASCII_STRING_MARSHALLER);
                  fixedHeaders.put(key, "primary.secondary");
  
                  headers.merge(fixedHeaders);
  
                  super.start(new SimpleForwardingClientCallListener &lt; RespT &gt; (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);
              }
          };
      }
  }
  </code></pre>
  
  <p><strong>EDIT: Added the custom header using this way successfully</strong></p>
  
  <p>Now in my grpc call, I am calling like this</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I have built the above request and calling it in the pseudo call as below.</p>
  
  <pre><code>Iterator&lt;Model&gt; dataItems = service.getItems(SOMERequestBuilderObj);
  </code></pre>
  
  <p>I am trying to add a custom header like this
  <code>"Grps-Matches-Key : primary.secondary"</code></p>
  
  <p>In Rest API call I would have added this as a header like</p>
  
  <pre><code>builder.header("Grps-Matches-Key", "primary.secondary");
  </code></pre>
  
  <p>Hope this helps.</p>