Since I started to do API development, I'm looking for the right API test tool. At first, I didn't want to use the chrome extension, wiztools tools, and then tried once Postman after the stop, but also bought a paid jetpacks. After I launched team Sync Beta, I promoted the tool to the team as an API document. See the Chinese Network on the tool on the article is not much, so decided to write a small article introduction.
First, basic functions
The features of postman are described in the documentation. But the document is a little verbose, here is a brief introduction to the main interface, the introduction function is mentioned.
Collections: In Postman, collection similar folder, you can put the same project request in a collection easy to manage and share, collection can also build folders. If you do API documentation, you can have a single request for each API, and if you want to test all of the inputs, you need a single request for each one. Here I've created a new example for the entire process, with five APIs corresponding to five requests. This collection can be https://www.getpostman.com/collections/96b64a7c604072e1e4ee
imported into your own postman.
The above black character registration is the name of the request and will be shown below if there is a requested description. The following blue word is the result of a saved request, click on the parameter and return value that can load a request. I will use this feature to show the various return values in different situations for colleagues who do the client. The button to save the request is at 15.
Choose the HTTP method of the place, a variety of common uncommon very full.
The request URL, two curly braces indicates that this is an environment variable, you can select the current environment at 16, the environment variable will be replaced with the value of variable in the environment.
Click to set the key and value of the URL parameter
Click Send Request
Click Save Request to collection, if you want to save as, you can click on the right down arrow
Set the authentication parameters, you can use OAuth and the like
Custom HTTP Header, some because Chrome is willing to be unable to customize the need to install another plugin interceptor, on the 16 row of satellites there
Set the request body,13 it shows the body content.
Scripts executed before the request is initiated, such as the two random variables in the request body, are generated temporarily before each request.
Test performed after receiving response, the results of the test will be displayed at 17 location
There are four forms to choose from, Form-data is used primarily for uploading files. X-www-form-urlencoded is the format commonly used in forms. Raw can be used to upload JSON data
Returns the format of the data, pretty can see the formatted Json,raw is the unprocessed data, preview can be previewed HTML page
Click here to save the request to the 2 location
To set environment variables and global variables, click the X on the right to see the current variable quickly.
Test the results of the execution, altogether several tests, through several.
This interface is the main content of the free version, compared with other API testing tools, is good enough. If you want to use automated testing, you need to buy $9.99 jetpacks, temporarily do not want to buy, you can try the team version of Postman. Now it is free to try, not only has the function of jetpacks, but also can synchronize with other accounts collection.
Second, testing tools
The test tool consists of three parts, the pre-request that ran before the request was initiated, the test that was run after the reply was received, and the collection that ran all the requests at once Runner
1. Pre-request
The authoring interface for Pre-request is as follows:
The Pre-request and test languages are Javascript,postman execute code in a sandbox, and the libraries and functions provided to the user can be viewed here. The common functions can be implemented by the Code snippets on the right, which can be inserted into the area of the codes.
You can see the functions used in pre-request in two ways, setting environment variables and setting global variables. The pre-request of this request is to generate a string as a random user name before registering.
postman.setEnvironmentVariable("random_username", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
Other uses include getting the current timestamp before initiating the request and putting it in the parameter:
postman.setEnvironmentVariable("unixtime_now", Math.round(new Date().getTime()/1000));
Of course, it can also be used to generate validation strings. In short, things that need to be manually modified before the request is made can be considered for automatic scripting.
2. Test
The writing interface for test is as follows:
The snippets of test is much richer than pre-request, such as checking the status code, checking the response string, validating the JSON, checking the header, and limiting the response time.
If you need to save the data that the server responds to, it needs to be done in a later request.
In the test in the diagram, I first checked the status code to 200, then parsed the returned JSON, and set the token in the environment variable to the token in the JSON.
3. Collection Runner
After writing a lot of tests, you can use collection Runner to run the entire collection, and the entry is in the Runner of the top row of the main interface. Select collection, environment, and optionally load JSON and CSV as the data source if necessary. Click Start Test Run to see the results.
Here you see a total of 5 requests, each with a test, all pass. (Although the return of the last request is 403, the expected return value for this request is 403, so it is also pass)
Iii. examples
Finally, take a look at the routines I'm using. This example is a very simple small system, the user can register and login, and then create a new recharge card in the system, and to recharge the card. The entire process is as follows:
1. Registration
Generate a random string as username and nickname
postman.setEnvironmentVariable("random_username", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
Initiating a request
POST /index.php/users HTTP/1.1Host: postmanexample.sinaapp.comCache-Control: no-cachePostman-Token: 76791813-aac2-71fb-cad4-3e737f37c4d0Content-Type: application/x-www-form-urlencodedusername=2mjk&password=123456&nickname=2mjk
Run tests, check results
tests["Status code is 201"] = responseCode.code === 201;
2. Login
Initiate a request directly with the environment variable you just generated
POST /index.php/authentication HTTP/1.1Host: postmanexample.sinaapp.comCache-Control: no-cachePostman-Token: aac7d0ac-e0e3-ecf2-39da-b8dca672e3d7Content-Type: application/x-www-form-urlencodedusername=2mjk&password=123456
Run the tests, check the results, and record the returned tokens
tests["Status code is 200"] = responseCode.code === 200;var data = JSON.parse(responseBody);postman.setEnvironmentVariable("token", data.token);
3. Add a card
Sir, a card number and a card name.
postman.setEnvironmentVariable("random_cardno", Math.round(Math.random()*9999999));postman.setEnvironmentVariable("random_cardname", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
The request is then made, where the token that was just acquired is called and placed in the header's custom field as authentication (the SAE cannot use the authorization field, and the reason is unclear)
POST /index.php/cards HTTP/1.1Host: postmanexample.sinaapp.comX-Authorization: d4c4a0b7b36c73e7a13b7e24a596093bCache-Control: no-cachePostman-Token: d44d573f-f17a-366c-2cd7-1d5b8b709233Content-Type: application/x-www-form-urlencodedcardno=1385526&desc=2mo8
Run Tests
tests["Status code is 200"] = responseCode.code === 200;
4. Query the card you just generated
The request was made and the card number that was just generated was called
GET /index.php/cards/1385526 HTTP/1.1Host: postmanexample.sinaapp.comCache-Control: no-cachePostman-Token: 1e5aca57-c3bb-7404-2791-c639cd60b5c8
Run validation, compare with the card name that you just generated, and record the ID of the new card
var data = JSON.parse(responseBody);tests["check cardname"] = data.desc === environment.random_cardname;postman.setEnvironmentVariable("new_card_id", data.id);
5. Recharge
Initiated the request, using the new card ID just obtained
POST /index.php/deposit HTTP/1.1Host: postmanexample.sinaapp.comX-Authorization: d4c4a0b7b36c73e7a13b7e24a596093bCache-Control: no-cachePostman-Token: 388c95e0-b5ce-9bbf-5816-084db7523384Content-Type: application/x-www-form-urlencodedcardid=1&amount=10
Run verification (due to the new user, no balance, unable to recharge the card, so return 403 Forbidden)
tests["Status code is 403"] = responseCode.code === 403;
P.S. postmanexample.sinaapp.com
This site is real, you can import my uploaded collection ( https://www.getpostman.com/collections/96b64a7c604072e1e4ee
) into your own postman, and set the environment variable url
for http://postmanexample.sinaapp.com/index.php
, you can run this collection see the effect.
API test Tool Postman Use tutorial