Sometimes you can't beat manual testing, and you can still do that with gRPC.Learn how.
Hey folks, so this is just a quick video to look at how we can debug gRPC services locally using Postman. Postman is a really great tool for making HTTP requests from your local machine and recently they've also added support for gRPC requests as well. For this example we're going to use a Hello World application that we built in a previous module.
When creating a new gRPC request in Postman, we just go to New and then gRPC. The first thing you need to do is provide the service definition to Postman, and that's how it will know the structure of the RPCs and the requests and the responses. There are two ways you can do that.
The first way is to provide a protobuf file that we've written from our local machine. To do that, it's as easy as dragging the file into Postman and then that will actually import the file. Once that's done we can select a method - we'll just select hello.proto and then this should give us a list that will read the file and generate a list of the RPCs that are registered on this protocol. Here we've got 'say hello' which is our RPC for this.
We can specify the URL (localhost in this case). We go to our message definition, this is where we'll add the request structure that we want to send. This is just a JSON representation of the protobuf message. For our hello service we just have a name in the request so we can just add name like this. It does have an autocomplete feature which is nice as well.
Making a request is as easy as clicking invoke and we get our response back as well in the JSON representation. Postman has some other good features as well. Similar to other HTTP requests, it has authorization if we want to add auth onto our request, it has metadata that are similar to headers in regular HTTP requests so these are key-values and we can add anything in here.
While we're just using localhost for now, if we were calling a remote server, we might specify a different URL. Also if our service is using TLS we can toggle this to enable TLS. If our server is using mutual TLS we might want to configure the certificates, so we can do that here as well.
That's how we make requests by specifying the protobuf files themselves, but there is another way to do this. Maybe we don't have the protobuf file on our machine or it's not easy to access, maybe we're using multiple files in a complex setup and it's not easy to import them all. The other option is to use server reflection.
Server reflection is a service provided by gRPC that basically exposes the details of our service publicly. This might be useful in environments where the schema is evolving regularly or when developing against gRPC services where we don't know the definition. However, it's worth noting we wouldn't want to enable this in production just because this will expose publicly our service and information about our service.
To enable server reflection (this is a package in the Go gRPC library), we just need to call the register function from the reflection package. To do that we just do reflection.Register and pass in our gRPC server. After importing reflection and running go mod tidy, we'll have access to the register function that takes our gRPC server and registers these servers behind the scenes.
Now if we rerun our server and go back to Postman, in the service definition we can change to use server reflection instead of importing a protobuf file. This will read the service information from the server itself. We now have two different types of services because we've registered two services on our gRPC server. The 'say hello' will still work as expected, but we also have the server reflection info so we can use this to get information about the server.
So that's it - making a request to a gRPC service via Postman is as simple as that. I hope you found this video useful and I'll see you in the next one where we'll look at gRPC curl.