50 / 65 · 14 API Testing Fundamentals · gRPC Fundamentals← prev⊞ allnext →☰ Read as one page
7.3Protocol Buffers
gRPC services are defined in .proto files:
syntax = "proto3";
package user;
service UserService {
rpc GetUser(GetUserRequest) returns (UserResponse);
rpc ListUsers(ListUsersRequest) returns (ListUsersResponse);
rpc CreateUser(CreateUserRequest) returns (UserResponse);
rpc UpdateUser(UpdateUserRequest) returns (UserResponse);
rpc DeleteUser(DeleteUserRequest) returns (Empty);
}
message GetUserRequest {
string id = 1;
}
message UserResponse {
string id = 1;
string name = 2;
string email = 3;
string role = 4;
string created_at = 5;
}
message ListUsersRequest {
int32 page = 1;
int32 per_page = 2;
}
message ListUsersResponse {
repeated UserResponse users = 1;
int32 total = 2;
}
The .proto file is the contract. Both client and server generate code from it, ensuring type safety.