# Class GrpcWeb

java.lang.Object
com.codename1.io.grpc.GrpcWeb

public final class GrpcWeb extends Object

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`) carrying `grpc-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

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `CONTENT_TYPE`
  Content type for binary gRPC-Web payloads.
  `static final int`
  `FLAG_TRAILER`
  gRPC-Web frame flag set in the trailer frame's first byte.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static <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 invokes `callback` with the decoded response (or a transport-failure marker).

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### CONTENT_TYPE

    public static final String 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:
      - Constant Field Values

  - ### FLAG_TRAILER

    public static final int FLAG_TRAILER
    gRPC-Web frame flag set in the trailer frame's first byte.
    See Also:
      - Constant Field Values

- ## 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 java.io.IOException
    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:
      `java.io.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 for `grpc-status` / `grpc-message`. Public so callers can replay canned responses in unit tests.