Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

WangScaler: A writer with heart.

Declaration: uneducated, if there is a mistake, kindly correct.

I’m sure every backend programmer has to test their interfaces. There are a lot of tools to test them. You’ve probably heard of Postman. Testers also use it to test our interfaces. You probably already use Postman, but do you really use it? The following screenshot is the latest version of Postman, if there is any inconsistency, can be updated to the latest version.

Simple use

I have a login interface http://localhost/love/login, for example, the request is a post request, and the parameters of need in the body carries the username and password.

We just need as shown in the picture below

This way is the most basic function, everyone can use and use, although you only know this way can also solve most problems, but it will be very inconvenient. Come and see with me what else it can do.

The header is set

As well as a front-end access interface menu access http://localhost/love/menu, but need in the header to token to access the interface. The token is the data returned by the login interface above.

We just need to add Headers. Of course, you can also set the Authorization.

A cookie is set

Click send under Cookies Settings.

The environment variable

To our local development environment url prefixes are http://localhost and formal environment may be https://www.scalerwang.com, if each request with this is quite a trouble, is there any way to deal with these variables? Yes, Postman can set environment variables.

Start by adding environment variables.

Set the correct address for the environment variable.

Finally, use environment variables.

The above is the local environment. When we test the interface of the formal environment, we just need to switch the environment variable in the upper right corner.

The global variable

Tokens obtained by login requests like the one we simply used above will be used by most subsequent interfaces, so we can’t just log in again after the token expires and assign the new token value to all requests. In this case, we can dynamically extract the token of the login interface and assign the value to the global variable. Other interfaces directly use the global variable to request.

On the login interface, the obtained token is set as a global variable.

Write the above code in Test. When writing, there is a prompt on the right side of the interface. You can select the appropriate code according to the prompt. Assert that the response body has this field, and store it in the global variable. Since my token is returned in the form of a header, it is extracted in the header here. Next, you can see that the global variable for the local environment is already there, and you can use it dynamically on other interfaces.

assertions

In fact, we have already used assertions, assertion header has the Authorization field. Next we assert the status code 200 in the get menu interface, you can set the assertion according to your needs. We can also write the assertions as prompted on the right.

grouping

Postman may be an API for multiple applications, or it may be multiple interfaces that are tested together, which can be grouped.

An application API may belong to different modules, or you can right-click to Add Folder.

In the upper right corner of the interface Save, you can Save the interface in the corresponding group!

Run

Click Run to the right of group User to test the whole group. Test the correctness of the interface against the assertions of Test.

The dynamic parameters

In addition to tokens, which can be retrieved from the interface, there are other dynamic parameters, such as timestamps, or GUids, and so on. You can use Postman’s built-in parameters directly. There are five of them, but there are many more.

You can also customize it.

conclusion

All in all, Postman is a powerful feature that you can’t do without in your daily development. If you haven’t used it before or just use its send requests, you can try out some of the great features that you don’t know about.

Come all come, click “like” and then go!

Follow WangScaler and wish you a promotion, a raise and no bucket!