본문 바로가기
Mobile/Android

[Android] grpc 에서 header 추가

by WooHey 2022. 10. 22.
Metadata header=new Metadata();
Metadata.Key<String> key = Metadata.Key.of("token", Metadata.ASCII_STRING_MARSHALLER);
header.put(key, "aaa");



ManagedChannel locationChannel = ManagedChannelBuilder.forAddress(GRPCConst.URL.LOCALHOST, GRPCConst.URL.LOCALHOST_PORT).usePlaintext().build();
ManagedChannel dispatcherChannel = ManagedChannelBuilder.forAddress(
        GRPCConst.URL.DISPATCHER_HOST, GRPCConst.URL.DISPATCHER_PORT)
        .usePlaintext()
        .intercept(MetadataUtils.newAttachHeadersInterceptor(header))
        .build();

https://stackoverflow.com/questions/71829925/tests-failing-when-using-clientinterceptor-to-add-headers-to-the-stub

 

Tests failing when using ClientInterceptor to add headers to the stub

In the following code MetadataUtils.attachHeaders is deprecated (I'm using grpc 1.45.1): "request without bearer token should fail" { val channel: ManagedChannel =

stackoverflow.com