grpc-transcode
Translates HTTP and JSON requests into gRPC calls and back.
grpc-transcodePriority 506What it does
grpc-transcode lets HTTP clients call a gRPC backend. The fields of the request's JSON body become the message of one call, to the method under Method of the service under Service. Both are defined in a proto, which the plugin names by its ID under Proto ID.
The route needs an upstream whose scheme is grpc. The ID under Proto ID is the one the proto was given when it was saved, shown on its page, and not its name.
When to use it
- The orders service speaks gRPC only, and the web shop and the partners call it with JSON over HTTP.
- A mobile app that carries no gRPC library asks for an order with
GetOrder, sending the order'sidin a JSON body. - A team moves its orders backend from REST to gRPC, and the HTTP paths the clients already call stay as they are.
Not for browsers that speak gRPC-Web themselves — see grpc-web (Lets gRPC-Web browser clients reach a gRPC upstream), reshaping a JSON body for a backend that reads JSON — see body-transformer (Rewrites request and response bodies with templates) or changing the path, Host or headers the backend receives — see proxy-rewrite (Rewrites the request URI, method, host and headers before proxying).
Set it up
Translate JSON requests into calls to a gRPC service method
proto_id is the ID the engine gave the Proto object when it was saved, shown on the Proto's page, and not the Proto's name; that Proto defines orders.Orders, and the route's upstream scheme is grpc.
Under Basics pick your proto in Proto ID, set Service to orders.Orders and set Method to GetOrder. Under Behavior set Deadline to 3000 milliseconds.
{
"proto_id": "00000000000000799563",
"service": "orders.Orders",
"method": "GetOrder",
"deadline": 3000
}
Things to know
- Service — The call reaches the backend under the service's full name. A backend that does not serve
orders.Ordersanswers it, and the client gets501withunknown service orders.Orders. - Proto ID — An ID the gateway does not hold is saved without complaint. Every request then reaches the backend untranscoded, and a gRPC backend answers
502. - Proto ID — The ID is text: a number,
7000007for example, is refused when you save. - Service — A name the proto does not define is saved without complaint, and every request is answered
503. - Method — An empty Method is refused when you save.
- Protobuf options — When two words of one family are stored, the one that sorts last wins. Left empty, the four default words apply.
- Response body limit — A longer answer is cut before it is decoded, and the client can receive
200with empty or partial fields. - A GET whose query string carries the fields,
id=42for example, is transcoded too: it reaches the backend under the service's name, as a POST with a JSON body does. - AdviceKeep the proto beside the backend's own source and update both together, so the gateway and the backend read one contract.
Settings
The fields as the console shows them, section by section.
Basics
proto_idstringRequiredThe id of the Proto that defines the service and method below. The engine assigns that id when the Proto is created, so it is not the Proto's name. An id the gateway does not hold is saved without complaint, and every request then reaches the upstream untranscoded, which a gRPC upstream answers with 502.
pattern
^[a-zA-Z0-9-_.]+$servicestringRequiredThe full name of a service defined in that proto, package included, such as hello.HelloService. A name the proto does not define is saved without complaint and every request is answered 503.
methodstringRequiredThe name of the method inside that service.
Behavior
pb_optionarray of stringsdefault ["enum_as_name","int64_as_number","auto_default_values","disable_hooks"]Decoding options, at most one word per family: enums, 64-bit integers, absent fields and hooks. When two words of one family are stored, the one that sorts last wins. Left empty, the four default words apply.
deadlinenumberdefault 0The call's deadline sent to the upstream, where zero or less sends none.
show_status_in_bodybooleandefault falseOn a failed call the decoded status details are written into the response body as well.
status_detail_typestringThe message type the status details are decoded as, where empty leaves them as they arrived.
Advanced
max_resp_body_sizeintegerdefault 67108864The largest response in bytes held for transcoding. A longer response is cut before decoding, and the client can receive 200 with empty or partial fields.