The main use case being "authorization" e.g. via JWT.
import grpc
from your_protobuf_package import my_service_pb2_grpc, my_service_pb2
# Step 1: Create a channel to the server
channel = grpc.insecure_channel('localhost:50051')
# Step 2: Create the stub for the service
stub = my_service_pb2_grpc.MyServiceStub(channel)
# Step 3: Prepare the request object
request = my_service_pb2.MyRequest()
# Step 4: Create metadata with Authorization header
token = "your_jwt_token_here"
metadata = [("authorization", f"Bearer {token}")]
# Step 5: Make the gRPC call and pass metadata
try:
response = stub.MyMethod(request, metadata=metadata)
print("Response received:", response)
except grpc.RpcError as e:
print(f"gRPC error: {e}")