I have added below new code in protobuf file and compiled to get the generated grpc_pb files.
service EchoService {
rpc Echo(EchoMessage) returns (EchoMessage) {
#-----New code start-----
option (google.api.http) = {
post: "/v1/echo"
body: "*"
};
#-----New code end------
}
}
From cURL command executed below command
curl -X POST -k https://localhost:10000/v1/echo -d '{"Key": "Value"}'
After making above request, not able to get the proper response.
My doubt is, any server side code changes needed to prepare the response to send back to caller? If so, Please suggest me with the code (Java) & also how to make request. If not, how we need to make http request to grpc?
Working example is much appreciated.
gRPC servers use a binary encoding on the wire (protocol buffers, or "protobufs" for short). So they are basically impossible to interact with using regular curl (and older versions of curl that do not support HTTP/2 are of course non-starters).
gRPC benefits gRPC uses HTTP/2 under the covers, but gRPC does not expose any of HTTP/2 to the API designer or API user. gRPC has already made all the decisions on how to layer the RPC model on top of HTTP so you don't have to—those decisions are built into the gRPC software and generated code.
In gRPC, you can use any preferred language to implement the client side. View the code on Gist. As shown in the above code snippet, the first step is to set up the connection to the server and initiate the client. Then, we can call the client's 'GetFeature' method to invoke the remote method.
In order to test gRPC server without client, we have to use grpcurl
not curl
. Please take a look at https://github.com/fullstorydev/grpcurl
However, based on my experience there is a requirement to make it works. First, please ensure that your service support Reflection, you can read about it from https://github.com/sourcegraph/gophercon-2018-liveblog/issues/27. There are different ways of doing Reflection across programming languages. My advice is, just do this for the development phase, otherwise, people may querying your gRPC endpoint. Maybe you can use if()
to make a conditional block for it. For Golang, i did this
import "google.golang.org/grpc/reflection"
if os.Getenv("GO_ENV") == "development" {
reflection.Register(s)
}
then, you need to know available services in your gRPC server. There is two way you can know the structure. First You can read them from your proto file, second by executing command grpcurl localhost:10000 list
packageName.Service/rpcMethodName
. So, based on your proto, it should be something like EchoService/Echo
or if you have package name it will be packageName.EchoService/Echo
grpcurl localhost:10000 list
, just type those command and it will output service path.The last thing to note when you test it locally and you don't setup SSL/TLS, please use -plaintext
option, otherwise, it will tell you that TLS handshake failed.
Example command, based on your proto, a call in local will looks like:
grpcurl -plaintext -d '{"Key": "Value"}' 127.0.0.1:10000 EchoService/Echo
Hope it helps.
UPDATE 30 June 2020:
After a few months working with gRPC I found another interesting gRPC tool:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With