Search code examples
pythonprotocol-buffersgrpc-pythonprotobuf-python

How to get header from grpc responce with python


I am trying to get header of the gRPC response with the following code, and it doesn't work:

response = stub.GetAccounts(users_pb2.GetAccountsRequest(), metadata=metadata)

header = response.header()

This is what this header looks like in Kreya, I'm trying to get it in python:

Header in Kreya

Does anyone know how to get the same header in python?


Solution

  • I suspect (! don't know) that you can't access the underlying HTTP/2 (response) headers from the (Python) gRPC client.

    You can configure various environment variables that expose underlying details (see gRPC environment variables) and perhaps GRPC_TRACE="http" GRPC_VERBOSITRY="DEBUG".

    If the headers were actually gRPC metadata, you can use Python's with_call and call.initial_metadata and call.trailing_metadata as shown in the gRPC metadata example here.