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

Protocol BuffersGrpc

Protocol Buffers Problem Overview


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

Protocol Buffers Solutions


Solution 1 - Protocol Buffers

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 {

}

Solution 2 - Protocol Buffers

You also can use predefined:

import "google/protobuf/empty.proto";
package MyPackage;

service MyService {
  rpc Check(google.protobuf.Empty) returns (google.protobuf.Empty) {}
}

Solution 3 - Protocol Buffers

you can also use another bool property inside the Reply structure. like this

message Reply {
  string result = 1;
  bool found = 2;
}

so if you dont find the result or some error happened you can return from the service class this

return new Reply()
{
   Found = false
};

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoneView Question on Stackoverflow
Solution 1 - Protocol BuffersNoneView Answer on Stackoverflow
Solution 2 - Protocol BuffershdnnView Answer on Stackoverflow
Solution 3 - Protocol BuffersdkokkinosView Answer on Stackoverflow