What is the Postman

Postman is a chrome plugin for interface testing, whether front-end, backend or testers can use Postman to test the interface, it is very convenient to use.

Postman installation

Official website download (Over the Wall)

www.getpostman.com/downloads/

Blue cloud played

www.lanzous.com/i2en5xc

Postman common functions

Once installed, we’ll open Up Postman and see that the interface is split into two parts: the collection, which we’ll cover later, on the right, and the Request Builder, which we’ll cover now, on the left. In The Request Builder, we can use Postman to quickly assemble any request we want. Generally speaking, all HTTP requests are split into four parts: URL, Method, headers, and body. Postman has targeted tools for each of these.

URL

To assemble a Request, the URL is always the first thing you need to fill in. In Postman, urls that you have entered can be automatically completed by pull-down. If you click on the Params button, Postman will bring up a key-value editor, where you can enter the Parameter of the URL, and Postman will automatically add it to the URL for you. On the other hand, if your URL already has parameters, Postman will load the parameters automatically when you open the key-value editor

Headers

Clicking on the ‘Headers’ button also brings up a key value editor for Postman. Here, you can add any Header attribute you want, and Postman does a nice auto-complete for us, where you type in a letter, and you can select the standard Atrribute you want from the drop-down menu

Method

Postman supports all methods, and once you select a Method, the Postman Request Body editor automatically changes based on your selection.

Request Body

If we want to create a request that is similar to a POST, we need to edit the request Body. Postman provides four methods of editing the request Body, depending on the Body type:

  • form-data
  • x-www-form-urlencoded
  • raw
  • binary

(We can transfer files here)

Postman advanced usage

Colletions (Collection of interfaces)

In the development process, it is possible to encounter the situation of simultaneous development and maintenance of multiple projects. Postman provides a friendly function of Colletions, similar to project folders, which can classify interfaces belonging to the same category together for easy management and maintenance.

  1. Click NEW -> Select Collection to create a project space.

  1. Enter a project name and click Create.

Colletions Folder

Each project has multiple interfaces, some of which are generic, such as the user management interface, the article list interface, and Postman’s folder directory for detailed sorting.

  1. Select a project and click Add Folder

  1. Enter a directory name and click Create

Each interface can be grouped into a project or subdirectory of a project.

Environment(Environment variable)

Postman allows you to define your own Environment variables, most commonly in the form of variables for test urls, so that urls don’t change as your domain name changes, which is very convenient. In addition, you can define sensitive test values as environment variables, such as passwords. Next, see how to create a new set of environment variables by opening the admin entry for environment variables as follows:

Open the window for managing environment variables, enter a name, and add a set of key-value pairs, as shown below:

Environment variables should be referenced in double curly braces. You can select the corresponding environment variables in the drop-down box at the top right. Let’s test the variables of the Url we just added:

Set variables by script

Postman allows users to customize scripts and provides two types of scripts:

  • Pre-request Script: Run it before you execute the request. You can set the required variables in it
  • Tests: Request is performed after the Tests: Request is returned, and can either extract and filter the returned information or perform some validation operations

example

Get the user_id value in the following information:

// Suppose the server returns the following Body content: {"token": {"user_id": "2079876912", "access_token": "26A90E317DBC1AD363B2E2CE53F76F2DD85CB172DF7D813099477BAACB69DC49C794BAECEDC68331", "expires_at": "The 2016-06-22 T12:46:51. 637 + 0800", "refresh_token" : "26A90E317DBC1AD3CD1556CF2B3923DD60AEBADDCBC1D9D899262A55D15273F735E407A6BEC56B84", "mac_key": "4FAhd4OpfC", "mac_algorithm": "hMAC-SHA-256 ", "server_time": "2016-06-15T12:46:51.649+0800"Copy the code

Extract the user_id value from Tests and assign it to a global variable:

// Check whether it exists'user_id'Value tests ["Body contains user_id"] = responseBody.has("user_id");
if(tests["Body contains user_id"Var responseData = json.parse (responseBody); tests["value_user_id"] = responseData. Token. User_id / / set the global variable postman. SetGlobalVariable ("user_id",tests["value_user_id"]);
}else{
    postman.setGlobalVariable("user_id"."The default user_id");
}
Copy the code

Practical cases

Project interface classification management

Log in to get the token and set it as a global variable

The interface uses the token after login