Recently a lot of friends are asking postman how to use, now I go through sorting, share to everyone.
Postman is a powerful tool for API debugging and HTTP requests, and when you're ready to take a notepad and write the form form, you'll be shocked when you try Postman.
First Open, display the main interface as follows:
Here's a quick introduction to some of the button actions you might be using.
- import : Used to import the API request file that you or the team saved, in JSON format.
- new Folder : For API request classification, easy to manage.
- Save Request : Save your API request, and the return value can be stored.
- download : Download your test through the API request, team share, import. JSON format, which can be edited manually.
She allows the user to send any type of HTTP request, such as Get,post,head,put, delete, and so on, and can allow arbitrary arguments and Headers.
She supports different authentication mechanisms, including Basic Auth,digest Auth,oauth 1.0,oauth 2.0.
She can also respond to data that is automatically highlighted in the syntax format, including Html,json and XML.
Characteristics:
- Create + Test: Create and send any HTTP requests that can be saved in history to execute again
- Organize: Managing and organizing APIs with postman collections for more efficient testing and integrated workflow
- Document: Automatically generate API documentation based on the clollections you create and publish it as a canonical format
- Collarorate: Connect your team and your API via sync, as well as permissions control, API library
First, the request
The postman interface is divided into two parts: the request builder on the left sidebar to the right: quickly create almost all requests
4 parts of the HTTP request: URL, method,headers,body of the request.
Url
The first thing you need to set is the URL
Note: If you do not automatically decode to the URL when you enter a parameter, you can select the encodeURIComponent (which is usually automatically populated) after you select the right key for the parameter:
It is also possible to decode the parameters into the form of dictionary (usually automatically populated):
Some URLs have path variables, postman can automatically extract the path variable to a key
Click Headers Toggle:
When entering Key-value, there will be an automatic prompt for the bottom ramen plate:
Some headers and cookies are confidential, such as:
1. Accept-charset2. Accept-encoding3. Access-control-request-headers4. Access-control-request-method5. Connection6. Content-length7. Cookie8. Cookies. Content-transfer-encoding10. Date11. Expect12. Host13. Keep-alive14. Origin15. Referer16. TE17. Trailer18. Transfer-encoding19. Upgrade20. User-agent21. Via
Postman 0.9. After version 6, these restrictions can be lifted:
Click Interceptor in the upper right corner to install this:
Cookies
The separate packaged application runs in the sandbox browser and it cannot access the cookie settings within the browser. This restriction can also be extended using interceptors.
Method
Request body
The different body editor is divided into 4 regions, which have different controls depending on the body type.
mutipart/form-data is the default format that Web forms use to transfer data. You can simulate filling out a form and submit a form.
You can upload a file as a key value submission (such as uploading a file). However, the file is not saved as a history and can only be re-added each time a request is sent.
urlencoded
As with the previous face, note that you cannot upload files through this encoding mode.
This mode and form mode are easy to confuse. The Key-value in urlencoded will write to Url,form-data mode Key-value not write to the URL obviously, but directly commit.
Raw
The raw request can contain anything. All text that is filled in will be sent as requested.
Binary
Image, audio or video files.text files. Also cannot save the history, each time chooses the file, submits.
Second, the response
Ensuring the correctness of the API response is what you need to do most of the work. Postman's Response Viewer section will help you get the job done and make it simple.
The response of an API contains Body,headers, the response status code. Postman put body and headers in different tabs. The response code and response time are displayed next to tabs. Hover over the response code to see more detailed information.
Save Responses
Three views View body:
Pretty
JSON and XML are formatted for easy viewing. Clicking on the Url,postman inside will create a request:
Click the triangle on the left to collapse the expansion:
Postman Automatic Formatting The body must be guaranteed to return the correct content-type. If the API is not returned, you can set it by tapping force JSON.
Raw is text.
Preview some browsers will return HTML errors, which is convenient for finding the problem. Because of the sandbox restrictions, JS and pictures are not displayed in the IFRAME here. You can maximize the body window for easy viewing of the results.
Headers key-value form Show. Hover over the Headers tab for detailed HTTP instructions.
Authentication Authentication
Postman has a helpers that can help us simplify some repetitive and complex tasks. The current set of helpers can help you solve some authentication protocols problems.
Basic Auth
Fill in your username and password and click Refresh Headers
Digest Auth
is more complex than basic auth. Generates the authorization header with the currently filled value. So make sure the settings are correct before you generate the header. If the current header already exists, Postman will remove the previous header.
OAuth 1.0a
Postman's OAuth helper lets you sign up for an OAuth 1.0-based authentication request. OAuth doesn't get access tokens, you need to go to the API provider to get them. OAuth 1.0 can set value in the header or query parameters.
OAuth 2.0
Postman supports obtaining OAuth 2.0 tokens and adding them to requests.
Third, writting Test
Postman's tests tag can be used to write tests:
Essentially JavaScript code, you can set values for tests object. Here, using descriptive text as key, check the body for a variety of situations, and of course you can create as many keys as you want, depending on how many points you need to test. Tests will also be saved to collection with the request. API testing ensures that the front-end is able to work properly with the API, without having to guess where the problem is at the time of the error. After you have created test in the request's test, the results of the request,test are viewed in the body's test. Note: 1. The key description here must be unique, otherwise the same description will only execute the first one. 2. Key here can be used in Chinese. Example: tests[“Body contains user_id”] = responseBody.has(“user_id”)
Here the descriptive key is: Body contains user_id. The detection point is: Responsebody.has ("user_id"), meaning to detect whether the returned body contains the field "user_id".
See tests results in responses: The demerit shows each key, that is, the specific result of our test point, whether it is passed.
Testing Sandbox
The postman test is run in a sandbox environment and is independent of the app. See what is available in the sandbox, as seen in the sandbox documentation.
Snippets
Used to quickly add commonly used test code. You can customize the snippets.
viewing results
Postman executes tests each time the request is executed. The test results will show the number of passes on the tab of the tests.
Testing Sandbox
Testing examples
The test code executes after the request is sent and the responses is received.
1. Setting Environment variablespostman.setEnvironmentVariable("key", "value");
2. Setting Global variablespostman.setGlobalVariable("key", "value");
3. Check if the response body contains a stringtests["Body matches string"] = responseBody.has("string_you_want_to_search");
4. Detect if a value in JSON is equal to the expected value
var data = JSON.parse(responseBody);tests["Your test name"] = data.value === 100;
Json.parse () method to convert the JSON string to an object. Parse () checks in JSON format as a safe function. For example: Check the number of elements in the JSON (the length of the programs is detected here)
var data = JSON.parse(responseBody);tests["program‘s lenght"] = data.programs.length === 5;
5. Convert the XML body to a JSON objectvar jsonObject = xml2Json(responseBody);
6. Check if the response body is equal to a stringtests["Body is correct"] = responseBody === "response_body_string";
7. Test for the presence of an element in the response headers (e.g., content-type)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //getResponseHeader()方法会返回header的值,如果该值存在
Or:
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
The method above is case-insensitive. The following methods are case-sensitive.
8. Verify the value of status codetests["Status code is 200"] = responseCode.code === 200;
9. Verify that response time is less than a certain valuetests["Response time is less than 200ms"] = responseTime < 200;
10.name If a value is includedtests["Status code name has string"] = responseCode.name.has("Created");
Whether the status response code for the 11.POST request is a valuetests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
12. Very small JSON data validator
var schema = { "items": { "type": "boolean" }};var data1 = [true, false];var data2 = [true, 123];console.log(tv4.error);tests["Valid Data1"] = tv4.validate(data1, schema);tests["Valid Data2"] = tv4.validate(data2, schema);
Results:
Four, running collections
Postman allows you to run collection, you can run any number of times. Finally, the result of a whole operation is given. Will save the results of each run and provide you with a comparison of each running dismissal.
Select collection and select the environment. Click the Run button.
Add the JSON file where you remember.
Running the collection test runs in another window. If you need to modify something in the main window, the new window will read normally.
Postman Using Tutorials