title: How can I access header in a service?
body: <p>I'm trying to handle JWT-authentication in gRPC on my backend. I can extract the JWT in an interceptor but how do I access it in my service? I think it should be done with a CoroutineContextServerInterceptor but this doesn't work:</p>
  <pre class="lang-kotlin prettyprint-override"><code>val jwtKey: Context.Key&lt;String&gt; = Context.key(&quot;jwtKey&quot;)
  
  fun main() {
      ServerBuilder.forPort(8980).intercept(UserInjector).addService(MyService).build().start().awaitTermination()
  }
  
  object UserInjector : CoroutineContextServerInterceptor() {
      override fun coroutineContext(call: ServerCall&lt;*, *&gt;, headers: Metadata): CoroutineContext {
          val jwtString = headers.get(Metadata.Key.of(&quot;jwt&quot;, Metadata.ASCII_STRING_MARSHALLER))
          println(&quot;coroutineContext: $jwtString&quot;)
          return GrpcContextElement(Context.current().withValue(jwtKey, jwtString))
      }
  }
  
  object MyService : MyServiceGrpcKt.MyServiceCoroutineImplBase() {
      override suspend fun testingJWT(request: Test.MyRequest): Test.MyResponse {
          println(&quot;testingJWT: ${jwtKey.get()}&quot;)
          return Test.MyResponse.getDefaultInstance()
      }
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>coroutineContext: something
  testingJWT: null
  </code></pre>