How to mimic response headers in gRPC

Trailers in a Http response context

How to mimic response headers in gRPC #

gRPC trailers are a fascinating aspect of the gRPC that often receives less attention than the core request-response mechanism. They play a role in communication between gRPC clients and servers by providing additional metadata or information after the main response message.

What Are gRPC Trailers? #

Trailers are part of the HTTP/2 protocol that carry additional metadata about a resonse. They allow servers to send extra information with the response. Usually, this information doesn't fit in the response message itself and can't be added to HTTP protocol headers collection. This is because the protocol is one level above the gRPC response and we are interested in the gRPC context and content.

gRPC uses trailers by default for two scenarios:

  1. to support streaming

  2. to send a grpc status code after the content

For example, when an error occurs, the Status(grpc-status) and Status-Message are delivered in the Trailers section

So basically, if we remember how a HTTP response looks like, we have:

  1. Headers (or header fiels/header properties)
  2. Body - the actual response content
  3. Status code - indicating the status of our Response( 404, 200, etc.)

If we look at the picture below, we will notice that there are several gRPC responses inside the same response body. This is dependent of the gRPC method type that we have. For a Server-streaming method type we will have several reponses, and for all the rest, just one.

A possum parent and two possum kids hanging from the iconic red balloon

After all the responses, we see the gRPC trailers, which is sent only after all the response messages are sent.

Adding trailers in .NET #

In .NET gRPC trailers are represented like a Metadata type, which is basically a dictionary that allows us to add key-value pairs from the server implementation. In our code, the type ServerCallContext contains info about both the current request and the response, and allows us to manage a few things. One of those things is ResponseTrailers. So, to add something in the ResponseTrailers, we simply create a Metadata.Entry object and we pass the key and value. If we need to add more than one key and value, we can also do that.

        public override Task<GrpcResponse> SayHello(GrpcRequest request, ServerCallContext context)
        {
            Metadata.Entry myHeader = new Metadata.Entry("my-fake-header", "grpc-header");
            context.ResponseTrailers.Add(myHeader);

            return Task.FromResult(new GrpcResponse
            {
                Message = "Hello " + request.Name
            });
        }

Conclusion #

gRPC trailers are an often overlooked but invaluable part of gRPC. Their ability to carry additional metadata beyond the main response is instrumental in improving error handling, providing contextual information, and enhancing the flexibility of the protocol. Developers leveraging gRPC can harness trailers to transmit crucial details, streamline error handling, and enrich the client-server communication with valuable context. Understanding and utilizing gRPC trailers can significantly enhance the efficiency and effectiveness of gRPC-based applications.