How to define a GRPC service method without request parameter?

Viewed 24

Normally definde a GRPC method as follows which has a request parameter HelloRequest:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

But how to define a method without request parameter as following SayHello method:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello () returns (HelloReply) {}
}


// The response message containing the greetings
message HelloReply {
  string message = 1;
}
1 Answers

Google's Well-Known Types includes Empty

Given that you must have a message for requests and responses, I think it's a good idea to define them in your protos even if initially empty unless you're absolutely confident that they'll always be empty:

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {}

message HelloReply {
  string message = 1;
}
Related