Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about JSON #234

Closed
idc77 opened this issue Apr 6, 2023 · 3 comments
Closed

Question about JSON #234

idc77 opened this issue Apr 6, 2023 · 3 comments

Comments

@idc77
Copy link

idc77 commented Apr 6, 2023

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
Copy link
Contributor

jhump commented Apr 6, 2023

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
Copy link
Author

idc77 commented Apr 12, 2023

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.

@idc77 idc77 closed this as completed Apr 12, 2023
@jhump
Copy link
Contributor

jhump commented Apr 12, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants