https://github.com/fullstorydev/grpcurl
Install:
go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
Add code to enable server reflection:
import "google.golang.org/grpc/reflection" reflection.Register(s) // s is a grpc server
In Makefile:
srvaddr=0.0.0.0:50051 gcurl: grpcurl -cacert ./certs/ca_cert.pem \ -authority "check.test.example.com" \ -reflect-header 'auth_token: authd' \ $(srvaddr) $(args)
Run grpcurl using Makefile, passing arguments:
zzh@ZZHPC:/zdata/Github/zgrpc-go-professionals$ make args=list gcurl
grpcurl -cacert ./certs/ca_cert.pem \
-authority "check.test.example.com" \
-reflect-header 'auth_token: authd' \
0.0.0.0:50051 list
grpc.reflection.v1.ServerReflection
grpc.reflection.v1alpha.ServerReflection
todo.v2.TodoService
zzh@ZZHPC:/zdata/Github/zgrpc-go-professionals$ make args="describe todo.v2.TodoService" gcurl
grpcurl -cacert ./certs/ca_cert.pem \
-authority "check.test.example.com" \
-reflect-header 'auth_token: authd' \
0.0.0.0:50051 describe todo.v2.TodoService
todo.v2.TodoService is a service:
service TodoService {
rpc AddTask ( .todo.v2.AddTaskRequest ) returns ( .todo.v2.AddTaskResponse );
rpc DeleteTasks ( stream .todo.v2.DeleteTasksRequest ) returns ( stream .todo.v2.DeleteTasksResponse );
rpc ListTasks ( .todo.v2.ListTasksRequest ) returns ( stream .todo.v2.ListTasksResponse );
rpc UpdateTasks ( stream .todo.v2.UpdateTasksRequest ) returns ( .todo.v2.UpdateTasksResponse );
}
zzh@ZZHPC:/zdata/Github/zgrpc-go-professionals$ make args="describe todo.v2.AddTaskRequest" gcurl
grpcurl -cacert ./certs/ca_cert.pem \
-authority "check.test.example.com" \
-reflect-header 'auth_token: authd' \
0.0.0.0:50051 describe todo.v2.AddTaskRequest
todo.v2.AddTaskRequest is a message:
message AddTaskRequest {
string description = 1 [(.validate.rules) = { string: { min_len: 1 } }];
.google.protobuf.Timestamp due_date = 2 [
(.validate.rules) = { timestamp: { gt_now: true } }
];
}
Run grpcurl with test data:
gcurld: grpcurl -cacert ./certs/ca_cert.pem \ -authority "check.test.example.com" \ -reflect-header 'auth_token: authd' \ -rpc-header 'auth_token: authd' \ -d $(data) \ $(srvaddr) $(args)
zzh@ZZHPC:/zdata/Github/zgrpc-go-professionals$ make args=todo.v2.TodoService.AddTask data="''" gcurld
grpcurl -cacert ./certs/ca_cert.pem \
-authority "check.test.example.com" \
-reflect-header 'auth_token: authd' \
-rpc-header 'auth_token: authd' \
-d '' \
0.0.0.0:50051 todo.v2.TodoService.AddTask
ERROR:
Code: Unknown
Message: invalid AddTaskRequest.Description: value length must be at least 1 runes
make: *** [Makefile:70: gcurld] Error 66
zzh@ZZHPC:/zdata/Github/zgrpc-go-professionals$ make args=todo.v2.TodoService.AddTask data="'{\"notexisting\": true}'" gcurld
grpcurl -cacert ./certs/ca_cert.pem \
-authority "check.test.example.com" \
-reflect-header 'auth_token: authd' \
-rpc-header 'auth_token: authd' \
-d '{"notexisting": true}' \
0.0.0.0:50051 todo.v2.TodoService.AddTask
Error invoking method "todo.v2.TodoService.AddTask": error getting request data: message type todo.v2.AddTaskRequest has no known field named notexisting
make: *** [Makefile:70: gcurld] Error 1
zzh@ZZHPC:/zdata/Github/zgrpc-go-professionals$ make args=todo.v2.TodoService.AddTask data="'{\"description\": \"a task\"}'" gcurld
grpcurl -cacert ./certs/ca_cert.pem \
-authority "check.test.example.com" \
-reflect-header 'auth_token: authd' \
-rpc-header 'auth_token: authd' \
-d '{"description": "a task"}' \
0.0.0.0:50051 todo.v2.TodoService.AddTask
{
"id": "1"
}

浙公网安备 33010602011771号