gRPC-JSON transcoder
This filter should be configured with the name envoy.filters.http.grpc_json_transcoder.
This is a filter which allows a RESTful JSON API client to send requests to Envoy over HTTP and get proxied to a gRPC service. The HTTP mapping for the gRPC service has to be defined by custom options.
The protobuf to JSON mapping is defined . For gRPC stream request parameters, Envoy expects an array of messages, and it returns an array of messages for stream response parameters.
How to generate proto descriptor set
To generate a protobuf descriptor set for the gRPC service, you’ll also need to clone the googleapis repository from GitHub before running protoc, as you’ll need annotations.proto in your include path, to define the HTTP mapping.
Then run protoc to generate the descriptor set. For example using the test provided in the Envoy repository:
If you have more than one proto source files, you can pass all of them in one command.
The route configs to be used with the gRPC-JSON transcoder should be identical to the gRPC route. The requests processed by the transcoder filter will have path and POST
method. The route configs for those requests should match on /<package>.<service>/<method>
, not the incoming request path. This allows the routes to be used for both gRPC requests and gRPC-JSON transcoded requests.
Assuming you have checked out the google APIs as described above, and have saved the proto file as protos/helloworld.proto
you can build it with:
Sending arbitrary content
By default, when transcoding occurs, gRPC-JSON encodes the message output of a gRPC service method into JSON and sets the HTTP response Content-Type
header to application/json
. To send arbitrary content, a gRPC service method can use as its output message type. The implementation needs to set content_type (which sets the value of the HTTP response header) and (which sets the HTTP response body) accordingly. Multiple google.api.HttpBody can be send by the gRPC server in the server streaming case. In this case, HTTP response header Content-Type
will use the content-type
from the first .
gRPC-JSON forwards the following headers to the gRPC server:
x-envoy-original-path
, containing the value of the original path of HTTP request
Sample Envoy configuration
Here’s a sample Envoy configuration that proxies to a gRPC server running on localhost:50051. Port 51051 proxies gRPC requests and uses the gRPC-JSON transcoder filter to provide the RESTful JSON mapping. I.e., you can make either gRPC or RESTful JSON requests to localhost:51051.