Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for including custom metadata (HTTP headers) in method calls #8

Open
CCP-Zeulix opened this issue Oct 4, 2024 · 0 comments

Comments

@CCP-Zeulix
Copy link
Collaborator

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}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant