A Primer on Mocking gRPC services with Postman
Tags: beginners, testing, postman, grpc
Published: 2023-08-08 10:59:48

---
title: A Primer on Mocking gRPC services with Postman
published: true
description: 
tags: #beginners #testing #postman #grpc 
cover_image: https://blog.postman.com/wp-content/uploads/2022/01/Announcing-gRPC-Proto-@2x.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-08-08 10:46 +0000
---

As part of increasing the overall developer productivity, it is a good practice to mock backend services so that the lead time to start the frontend related tasks including, UI/UX development, can proceed without much dependency of a fully functioning backend server.

Below we can see how a gRPC request can be formed via Postman.

![https://assets.postman.com/postman-docs/v10/select-protocol-v10.gif](https://assets.postman.com/postman-docs/v10/select-protocol-v10.gif)

For submitting a valid gRPC request in Postman, a service definition and the method needs to be invoked. Depending upon the service definition, extra parameters like message payloads, metadata and other details can be attached along with the request.

There are two ways of setting up the service definition in Postman. One way is by uploading a .proto file and another way is by supplying the service endpoint in the URL field.

### Creating a service definition by uploading a proto file

One way how the service definition can be instantiated in Postman is by uploading the proto file physically. This can be a good way for the engineers to test the requests in case the service is not yet deployed. 

For this, the proto can be uploaded as below:

{% youtube Hl3oTt5vt0Y %}

Keep in mind that when the Import API button is clicked, then a new API is automatically created in the APIs section in Postman. See below that a New API collection ‘New API’ is created which has a service definition as `projects.proto` which is the uploaded proto in the previous step.

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxbtuikwzjx3a95mn6zq.png)

### Maintaining a service definition by server reflection

Server-reflection is a gRPC feature allowing clients to submit requests without having to have precompiled into the client side. Basically, what that means is that, if the server has server-reflection enabled, then the client can pull the available public methods by simply sending a request to the service endpoint.

In the example below, we make a request to a Postman gRPC echo service `[grpc.postman-echo.com](http://grpc.postman-echo.com)` Further info about making a gRPC request using server-reflection from Postman can be found [here](https://learning.postman.com/docs/sending-requests/grpc/first-grpc-request/#creating-and-invoking-a-request).

{% youtube yXq_m7YNUAc %}

### Sample Request and Response

Once the service definition is available, you can click on the `Invoke` button to invoke the remote procedures defined by the service definition. You can click on the `Use Example Messages` button which prepares a mock message body that needs to be sent along with the request.

Below is the screenshot showing the mock request and response.

![Mocking the gRPC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekg2921izi8qosuvbyts.jpg)

### Connecting a repository to maintain an API Lifecycle

Even though the developer can manually feed the `proto` files directly to Postman, a much shorter feedback loop can be achieved by directly linking a remote repository to fetch the service definitions.

Moving along, we will see how we can link a Bitbucket repo to fetch the service definitions. This flow will allow both the backend and frontend teams to collaborate real-time. A typical use case for this flow can be thought of as below:

- The backend team prepares the `proto` file and pushed it to the repository
- The frontend team can fetch the `proto` file directly from Postman. Postman takes care of maintaining the repo status so that the service definition is always in sync with the latest changes.
- Based on the `proto` files, the developer can then build a mock server. The mock server is just what the name suggests. It returns random fake data based on the latest service definition in the repo. The frontend team can then utilize this endpoint to move ahead with their development tasks.

Below is the actual flow how we can fetch the service definition from the repo. We see here that after we have an active link with the repository (in this case, Bitbucket), we can see the uncommitted changes and any pending changes yet to be pulled from the remote. Please note that once the repo is connected, Postman creates a couple of internal files under `.Postman` folder for tracking purposes. 

{% youtube 5A1ZpbM7_ds %}

### Creating Mock gRPC Servers

Once you have created an API (from the APIs option in the sidebar), Postman allows the users to publish the API so that it can be shared within the team.

In continuation from the example above, we will see how the user can create a mock server from the fetched service definition. While publishing the API, users need to [version the APIs](https://learning.postman.com/docs/designing-and-developing-your-api/versioning-an-api/versioning-an-api-overview/) which is a neat feature which ensures that a proper versioning is maintained for each change in the server definition.

{% youtube 1X4ovsSUghI %}

Now we will see how to use this published API to create a mock server and submit a valid request and receive the response as dictated by the service definition.

{% youtube 9kBKCeF0ekA %}

### Using Postman to test the gRPC response

Although Postman provides immense value to the development team to cut down on the lead time needed to develop features, it can also provide many different ways to test the actual responses received from the hosted service endpoints.

Below, we will be invoking procedures and using the scripts to test the responses. The service endpoint for testing is an actual functional endpoint `[projects-service.prd.svc.grepsr.net](http://projects-service.prd.svc.grepsr.net)` which allows server-reflection.

{% youtube ecpE-lSHMlE %}

### Further Reading

Even though we try to maintain this doc for any upcoming updates in the future, you can get the latest information from the official Postman docs [here](https://learning.postman.com/docs/sending-requests/grpc/grpc-client-overview/)