gRPC Basics Part 2: Rest and Swagger

Andrew Hayes
Level Up Coding
Published in
5 min readJul 1, 2019

--

Source: https://grpc.io/

This is Part 2 in a series of articles on gRPC that my colleague Scott Walker and I are working on. It continues on from Part 1, and we will show some examples of exposing a gRPC service using REST and auto-generating a swagger file for it too. The full code for all our examples can be found in our repo.

Didn’t you say in Part 1 that gRPC can replace REST?

I did, but sometimes you have to use REST. Maybe that’s for a third-party to integrate into your service, or even for your UI. Luckily we can generate REST endpoints to sit on top of our gRPC service using ‘protoc’.

Example B. Adding REST endpoints to a gRPC service

This example, an elementary gRPC Client and Server with REST is here: ‘grpc-example/B.grpc-with-rest’. When adding REST endpoints, we need to tell gRPC what URL to map to each function in the ‘.proto’ file. The updated definition of MakeBox with the URL mapping looks like this:

rpc MakeBox(BoxSpecification) returns (Box) {
option (google.api.http) = {
get: "/v1/make-box"
};
}

This means when we call the URI ‘/v1/make-box’ it will get passed through to the MakeBox function. To make use of the changes we need to generate our code again, in our example…

--

--