public final class GrpcWeb
- Object
- 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 and
receive the parsed GrpcResponse on the supplied callback.
Fields
public static final String CONTENT_TYPE = "application/grpc-web+proto" | Content type for binary gRPC-Web payloads. |
public static final int FLAG_TRAILER = 128 | gRPC-Web frame flag set in the trailer frame’s first byte. |
Methods
Inherited methods
Field details
CONTENT_TYPE
public static final String CONTENT_TYPE = "application/grpc-web+proto"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.FLAG_TRAILER
public static final int FLAG_TRAILER = 128gRPC-Web frame flag set in the trailer frame’s first byte.
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 callback with
the decoded response (or a transport-failure marker).
The baseUrl should not include a trailing slash; the
service is the fully qualified service path
(e.g. helloworld.Greeter) and method is the gRPC method
name (e.g. SayHello). The resulting URL is
<baseUrl>/<service>/<method>.
frame
public static <T> byte[] frame(T request, ProtoCodec<T> codec)
throws IOExceptionWraps 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
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 for
grpc-status / grpc-message. Public so callers can replay
canned responses in unit tests.