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

Remove result/error envelope in streaming RPC response #1254

Closed
adasari opened this issue Apr 29, 2020 · 9 comments
Closed

Remove result/error envelope in streaming RPC response #1254

adasari opened this issue Apr 29, 2020 · 9 comments

Comments

@adasari
Copy link
Contributor

adasari commented Apr 29, 2020

grpc-gateway wraps stream response in result/error envelope.

{"result": {"data": {}}, "error": {}}

Does result/error envelope required ? nice to have an option to skip result/error envelope ?
Could you help me to understand the reasons behind result/error envelope design ?

in case http byte stream apis (ex: download rpc), this extra envelope is not useful.

@adasari adasari changed the title Streaming RPC wraps the response in result/error envelope Remove result/error envelope in streaming RPC response Apr 29, 2020
@johanbrandhorst
Copy link
Collaborator

You should be able to use google/api/httpbody.proto as the return stream type and use https://github.com/grpc-ecosystem/grpc-gateway/blob/master/runtime/marshal_httpbodyproto.go and it will not wrap your messages. I've not tested this with streaming RPCs but it should work I think?

@adasari
Copy link
Contributor Author

adasari commented May 1, 2020

Sure. let me find some examples and try it. thanks.

@adasari
Copy link
Contributor Author

adasari commented May 1, 2020

i was reading https://grpc-ecosystem.github.io/grpc-gateway/docs/httpbody.html, can we use it with gRPC stream ? basically i am building download rpc which works as gRPC stream for clients and http byte stream for http clients.

@johanbrandhorst
Copy link
Collaborator

If it doesn't already work with streaming we should try to make sure it can work. Can you test it?

@adasari
Copy link
Contributor Author

adasari commented May 4, 2020

i couldn't find examples. Can you point me to right examples ? thanks.

@johanbrandhorst
Copy link
Collaborator

Doesn't look like we have any, but we have this: https://grpc-ecosystem.github.io/grpc-gateway/docs/httpbody.html

@adasari
Copy link
Contributor Author

adasari commented May 4, 2020

Seems not working.

 rpc Download(HelloRequest) returns (stream google.api.HttpBody) {
    option (google.api.http) = {
      post : "/v1/download"
      body: "*"
		};
  }

gRPC implementation

func (s *server) Download(req *testpb.HelloRequest, stream testpb.Greeter_DownloadServer) error {
	stream.Send(&httpbody.HttpBody{
		ContentType: "text/html",
		Data:        []byte("124353"),
	})

	return nil
}

response

{
    "result": {
        "content_type": "text/html",
        "data": "MTI0MzUz"
    }
}

mux is

mux := http.NewServeMux()
	gwmux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(CustomMatcher), runtime.WithOutgoingHeaderMatcher(CustomOutMatcher))
	runtime.SetHTTPBodyMarshaler(gwmux)
	mux.Handle("/", gwmux)

httpbody.HttpBody type conversion is not ok. hence else block is executed.

func (h *HTTPBodyMarshaler) Marshal(v interface{}) ([]byte, error) {
	if httpBody, ok := v.(*httpbody.HttpBody); ok {
		return httpBody.Data, nil
	}
	return h.Marshaler.Marshal(v)
}

@johanbrandhorst
Copy link
Collaborator

Okay, lets try and get this in for v2, if you'd like.

@johanbrandhorst
Copy link
Collaborator

This is fixed in v2

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

Successfully merging a pull request may close this issue.

2 participants