Class GrpcWeb
High-level gRPC-Web invoker used by generated @GrpcClient
implementations. Handles HTTP-level transport, gRPC-Web payload
framing (flags + length + payload), and trailer parsing
(grpc-status, grpc-message).
Wire details (from the upstream gRPC-Web spec):
- URL:
<baseUrl>/<service-fqn>/<method-name>(no trailing slash). - HTTP method:
POST. - Request
Content-Type:application/grpc-web+proto. - Request headers:
X-Grpc-Web: 1,X-User-Agent: grpc-web-cn1/<ver>. - Request body:
0x00 <length-be32> <payload>-- one data frame. - Response body: zero or more data frames (flags low bit 0)
followed by one trailer frame (flags high bit
0x80) carryinggrpc-status:<n>\r\ngrpc-message:<text>\r\n.
All methods are static. Generated impls call invokeUnary(String, String, String, String, Req, ProtoCodec, ProtoCodec, OnComplete) and
receive the parsed GrpcResponse on the supplied callback.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringContent type for binary gRPC-Web payloads.static final intgRPC-Web frame flag set in the trailer frame's first byte. -
Method Summary
Modifier and TypeMethodDescriptionstatic <Resp> GrpcResponse<Resp> decode(byte[] response, int httpCode, ProtoCodec<Resp> respCodec) Decodes a complete gRPC-Web response body.static <T> byte[]frame(T request, ProtoCodec<T> codec) Wraps a serialised request message in a single gRPC-Web data frame:[0x00][length-be32][payload].static <Req,Resp> void invokeUnary(String baseUrl, String service, String method, String bearerToken, Req request, ProtoCodec<Req> reqCodec, ProtoCodec<Resp> respCodec, OnComplete<GrpcResponse<Resp>> callback) Sends a unary gRPC-Web request and invokescallbackwith the decoded response (or a transport-failure marker).
-
Field Details
-
CONTENT_TYPE
Content type for binary gRPC-Web payloads. The text variant (application/grpc-web-text, base64-encoded) is not supported -- modern Envoy/gRPC-Web proxies all speak binary.- See Also:
-
FLAG_TRAILER
public static final int FLAG_TRAILERgRPC-Web frame flag set in the trailer frame's first byte.- See Also:
-
-
Method Details
-
invokeUnary
public static <Req,Resp> void invokeUnary(String baseUrl, String service, String method, String bearerToken, Req request, ProtoCodec<Req> reqCodec, ProtoCodec<Resp> respCodec, OnComplete<GrpcResponse<Resp>> callback) Sends a unary gRPC-Web request and invokes
callbackwith the decoded response (or a transport-failure marker).The
baseUrlshould not include a trailing slash; theserviceis the fully qualified service path (e.g.helloworld.Greeter) andmethodis the gRPC method name (e.g.SayHello). The resulting URL is<baseUrl>/<service>/<method>. -
frame
Wraps a serialised request message in a single gRPC-Web data frame:[0x00][length-be32][payload]. Public so unit tests can verify framing independently of the network path.- Throws:
IOException
-
decode
public static <Resp> GrpcResponse<Resp> decode(byte[] response, int httpCode, ProtoCodec<Resp> respCodec) Decodes a complete gRPC-Web response body. Iterates frames, concatenates data payloads, and parses the trailer frame forgrpc-status/grpc-message. Public so callers can replay canned responses in unit tests.
-