loading...
Cover image for Leveraging the power of Postman for API Development
IntegridSolutions

Leveraging the power of Postman for API Development

puncoz profile image Puncoz Nepal Originally published at blog.yipl.com.np ・5 min read

While developing API, either RESTful or GraphQL or SOAP, testing and debugging are crucial. But creating a form to consume those API, just for the sake of debugging, is not very efficient and effective. Hence, some kind of API client, capable of consuming or querying GET/POST or any other kind of HTTP request to the endpoint, is required.

Among the vast selection of GUI tools available for this purpose, I find Postman quite easy and really powerful. Here, I will list some of the advanced features of Postman to increase productivity as an API developer.

Postman Collections

Every time we send a request to some API endpoint, the request information (its URL, request parameters and response data ) is stored in the History Tab of the postman. It makes it easier to re-send the same request from history later as required. But as the number of requests increases, finding a particular request in the history tab will be time-consuming.

Postman Collections are a group of saved requests, organized in folders and sub-folders structure. This way we can manage API requests. Here is a guide on how we can create postman collections.

Besides this, there are other benefits to creating postman collections.

Sharing a Postman Collection

Postman collections we created can be shared with other teammates and people who are concerned with our API.

Sharing and publishing postman collection.

Sharing with Teams and other workspaces

Our workspaces can be shared with other teammates by inviting them in the workspace or by creating another common workspace. This way, the collections will always stay in sync for all teammates.

Sharing as a JSON file or URL

If you do not want people to add in your team, but still need to share postman collections, you can export collections as a JSON file or generate a URL of JSON data which afterward can be imported in postman as a collection.

Here is a more detailed guide to share a postman collection.

Creating and Publishing API Documentation

Postman generates and hosts browser-based API documentation from the collection which can be shared to privately as well publicly. This API documentation is beautifully formatted on a web page which can reduce extra efforts to document API separately. More about API documentation can be read from here.


Environment and Global Variables

Using the Postman variable for dynamic URL.

Variables in Postman are static identifiers used to store dynamic value to be used in requests and responses of API calls. A straightforward use-case of the variable is to define an API base URL for local, staging, and production servers. This way, we do not need to create three different API requests or change the URL manually. As shown in the figure above, we can use {{url}}, a postman variable storing different URLs for different environments.

Another useful use-case of the variable is to store the user authentication token. Storing user token in variable enables us to request another request by automatically sending an updated token from the variable.

To store authentication token from response to an environment variable token, the following scripts should be written in Tests tab of API request panels.

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.token);

More about the environment and global variables are in postman’s documentation.


Test Scripts

A very cool and useful feature of Postman is Test Scripts where we can write test cases for every API request. Postman supports javascript, hence we can use any javascript test library (such as jasmine, mocha, etc.) or postman’s native test library.

Test scripts in postman

A sample test scripts using the PostmanBDD library. Here, I stored PostmanBDD in a global variable with key postmanBDD. Below the Test Scripts to test a typical API response with Chai assertions.

var jsonData = JSON.parse(responseBody);
eval(globals.postmanBDD);
describe("response", () => {

    it("should return a 200 response", () => {
        response.should.have.status(200);
    });

    it("should have the json content type header", () => {
        response.should.have.header('content-type', 'application/json');
        response.should.be.json;
    });

    it('should return keys: title, content, id', () => {
        assert.containsAllKeys(jsonData,['title', 'content', 'id']);
    });

    it("'title' key should be provided title", () => {
        jsonData.title.should.be.an('string').and.equal(environment.create_title);
    });

    it("'content' key should be as provided", () => {
        jsonData.content.should.be.an('string').and.equal(environment.create_content);
    });

    it("should have 'id' property", () => {
        jsonData.id.should.be.an('number');
    });

});

This way we can test our API response to avoid manual testing. Test results can be seen in the Response panels of Postman. Read more at documentation.


Collection Runner

A collection runner is another useful feature of Postman. This allows us to run a group of API requests together within a collection with just a single click. This is useful when we have test scripts written and want to automate API testing. This will save a lot of time testing each and every request one by one. Read more.

postman runner

Collection runner in postman


Postman monitoring

Postman monitoring is another cool feature of the postman, which runs a collection just like postman runner does but periodically to check for its performance and response. We can set up a monitor to run as frequently as 5 minutes to check if all the requests in your collection are up and healthy.

Monitoring is priced per request made with some limited number of free monitoring calls per month.


Mock servers

Postman’s mock servers is a feature, especially useful for API consumer (front enders) to simulate each endpoint and its corresponding response and work with API before back enders provide actual API. Not just front-enders, API developers can also use mock servers to design and plan their API before diving into the actual development.


For more tips and tricks about Postman, follow these links


That's all about Postman. If you are using it in your API development share how are you using it? If you have any interesting tips and tricks about the postman, do share :)

Also if you have any other alternatives, share that as well in the comment.


This article was originally published at YoungInnovations Blog

Posted on May 20 by:

puncoz profile

Puncoz Nepal

@puncoz

Full-stack Software Engineer based in #Nepal. #AI #ML #Robotics #enthusiast #computer #science. Creating impact by using state of the art technology.

IntegridSolutions

We love what we do and we love helping others succeed at what they love to do. We focus on Web Design - Development, Marketing & Branding.

Discussion

markdown guide