fullstorydev / grpcui

An interactive web UI for gRPC, along the lines of postman
MIT License
5.24k stars 388 forks source link

Question about JSON #234

Closed idc77 closed 1 year ago

idc77 commented 1 year ago

When I use grpcui, it's able to receive JSON requests, forward them to my grpc server and receive a response and return JSON as well. It that something so easy to do? Why don't you release a standalone grpc to JSON gateway? Or have I been living under a rock for the last few years? I've always waited for grpc-web to be complete but it never is, yet here you have a middleware that simply constructs clients by reflection and posts and receives JSON objects. Mind blown.

How?

Can I simply add some magic package and have that functionality as well?

jhump commented 1 year ago

Why don't you release a standalone grpc to JSON gateway?

So you are asking for something that just translates the protobuf binary payloads to JSON? FWIW, streaming and other gRPC protocol features such as compression would not really work well with this.

It sounds like maybe you are wanting a gRPC-web client that uses JSON, paired with a server that uses JSON. This is currently problematic because https://github.com/grpc/grpc-web uses a JS protobuf library that does not support JSON. So a few things you might instead consider:

  1. You could use https://github.com/bufbuild/connect-es to create gRPC-web clients that support JSON and then also use an encoding.Codec in your server that supports JSON (if you are using a Go server anyway). The Codec interface is easy to implement for JSON: the two methods map to functions of the same name in the protojson package.
  2. You could use Connect instead of gRPC. It supports JSON out of the box. (Implementations currently available in TS/JS, Go, Kotlin/Android, and Swift/iOS).
idc77 commented 1 year ago

I'm just looking for a way to consume the grpc server on a web frontend. Idk what buf has to do with any of this. Everywhere I go in the grpc world I'm met with buf marketing. Forget I asked, it was a stupid idea. grpc is not fit for the web.

jhump commented 1 year ago

I'm just looking for a way to consume the grpc server on a web frontend.

@idc77, the "official" answer from the gRPC team is probably to use gRPC-Web: https://github.com/grpc/grpc-web However, it uses Protobuf binary encoding, not JSON (though it does have an option to use the Protobuf text format). The gRPC-Web protocol is a little different than gRPC so that it can support HTTP 1.1 and browser HTTP client capabilities. So it requires running a proxy in front of your gRPC server that translates from the gRPC-Web proxy. There is also an Envoy filter, so if you already run Envoy as an ingress/reverse proxy or as a sidecar (like in a service mesh), it can be configured to automatically translate gRPC-Web to gRPC.

grpc is not fit for the web.

You might check out Connect: it was designed for web and mobile use cases. So it should be a much better experience. It supports JSON, it has a curl-friendly protocol for unary RPCs, and works in web (TS/JS) and mobile (Kotlin and Swift).