Can I define a grpc call with a null request or response?

Mark Kahn picture Mark Kahn · Aug 2, 2015 · Viewed 74k times · Source

Does the rpc syntax in proto3 allow null requests or responses?

e.g. I want the equivalent of the following:

rpc Logout;
rpc Status returns (Status);
rpc Log (LogData);

Or should I just create a null type?

message Null {};

rpc Logout (Null) returns (Null);
rpc Status (Null) returns (Status);
rpc Log (LogData) returns (Null);

Answer

Mark Kahn picture Mark Kahn · Aug 2, 2015

Kenton's comment below is sound advice:

... we as developers are really bad at guessing what we might want in the future. So I recommend being safe by always defining custom params and results types for every method, even if they are empty.


Answering my own question:

Looking through the default proto files, I came across Empty that is exactly like the Null type I suggested above :)

excerpt from that file:

// A generic empty message that you can re-use to avoid defining duplicated
// empty messages in your APIs. A typical example is to use it as the request
// or the response type of an API method. For instance:
//
//     service Foo {
//       rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);
//     }
//

message Empty {

}