What is a Postman?

Postman started out in 2013 as a tool for Abhinav Asthana to solve API testing. As the number of users and demand for the tool exploded, Abhinav Asthana enlisted two former colleagues, Ankit Sobti and Abhijit Kane, to create the company Postman Inc.

Postman is now a collaborative platform for API development. Postman simplifies every step of building an API and simplifies collaboration so that the API can be created faster.

2. Use variables

Postman allows users to use variables when sending and receiving to improve productivity and readability (although only string values can be held, so complex data types need to be managed with the help of json.stringify () and json.parse ()).

For example, to set the domain name address as a variable in different runtime environments:

Postman supports the use of variables in different scopes and contexts, following the proximity principle, that is, if there is a variable name in both Global and Environment, it will take the name in Environment.

2.1 Variable scope applies to different scenarios in Postman

  • Global: Global variables can be used throughout the Workspace, and should be immutable because of uncontrollable environment and confusionGlobal constantsUse with caution.
    pm.globals.set("variable_key", "variable_value");
    pm.globals.get("variable_key");
    Copy the code
  • Collection: Collection variables are available in a single Collection and often have common business binding attributes, such as commodity attributes, membership levels, and common secret keys.
    pm.collectionVariables.set("variable_key", "variable_value");
    pm.collectionVariables.get("variable_key");
    Copy the code
  • Environment: Environment variables allow requests to be adapted to different environments, such as local, test, rehearsal, and production environments, and are often used to distinguish request addresses.
    pm.environment.set("variable_key", "variable_value");
    pm.environment.get("variable_key");
    Copy the code
  • Data: Data variables come from external CSV and JSON files and are used when running through Newman or Runner.
  • Local: Local variables are available only during the lifetime of a single request and are automatically destroyed upon completion.
    pm.variables.set("variable_key", "variable_value");
    pm.variables.get("variable_key");
    Copy the code

2.2 Editing global and environment variables

2.3 Editing collection variables

2.4 Using built-in dynamic variables

Postman has built-in dynamic variables for many common scenarios.

Note: Postman supports printing debugging information in pre-Request Script and Tests, just like in the browser console. You can use the command: Console.log (), console.info(), console.warn(), and console.error().

Console. log(' current timestamp: ', pm.variables. ReplaceIn ('{{$timestamp}}')); Console. log(' randomColor: ', pm.variables. ReplaceIn ('{{$randomColor}}')); Console. log(' randomIP: ', pm.variables. ReplaceIn ('{{$randomIP}}')); Console. log(' random name: ', pm.variables. ReplaceIn ('{{$randomFullName}}')); Console. log(' randomJobType: ', pm.variables. ReplaceIn ('{{$randomJobType}}')); Console. log(' randomCity: ', pm.variables. ReplaceIn ('{{$randomCity}}')); Console. log(' random image: ', pm.variables. ReplaceIn ('{{$randomImageUrl}}')); SQL > Select * from Azure where IP: 163.140.207.64 where name: Chester Funk where occupation: Coordinator Where city: Port Devinborough Where picture: http://placeimg.com/640/480Copy the code

Postman request lifecycle

In Postman, a complete Postman request lifecycle includes pre-request scripts and tests scripts in addition to the normal request and response. Postman includes a powerful Node.js-based Runtime that allows users to write JavaScript code in pre-Request Script and tests events.

3.1 Using scripts in pre-Request Script

A pre-request script is, as the name suggests, a script that is executed before the request is sent.

3.2 Sending a Request

Tip 1: Customize the path parameter with :id in the link

Tip 2: Cookies can be edited

3.3 Receiving a Response

Tip: Save the response

The saved results can be used as a case or documented for development purposes.

3.4 Using scripts in Tests

Postman supports test scripts to verify after a request has been responded that the request is as expected.

Example 1: Check whether the response status code is 200

pm.test("Status test", function () {
    pm.response.to.have.status(200);
});
Copy the code

Example 2: Verify that the returned business data (JSON) is as expected

Pm.test (" Request successful!" , function () { var jsonData = pm.response.json(); pm.expect(jsonData.message).to.eql('success'); });Copy the code

Use Postman to capture packets

There is a built-in proxy in the Postman application to capture HTTP requests.

  1. The Postman application listens for any calls made by the client application or the device.
  2. The Postman agent captures the request and forwards it to the server.
  3. The server returns the response to the client through the Postman agent.

4.1 Enabling Packet Capture

Local IP address:

Phone Settings:

4.2 Packet capture Effect

Use proxies

A proxy server is an application or system that acts as an intermediary between a computer and the Internet, or more specifically on behalf of clients and servers, making requests to websites, servers, and other Internet services.

In addition to passing information, agents can do more:

  1. Record all traffic between your machine and the Internet.
  2. Displays the contents of all requests, responses, cookies, and titles.
  3. Routes traffic to a specified Internet location.
  4. Debug the interface.
  5. Prevent direct attacks and ensure security.
  6. DevOps load balancing.

By default, Postman uses its own system agent. If you have a custom agent, it has a higher priority than its own system agent.

Use Collection Runner

A Collection Runner allows requests in a Collection to be run in a specified order. The Collection Runner records the request test results, and the script can pass data between requests.

7. Command line scaffold Newman

Postman provides the scaffold tool Newman to run Collection requests from the command line. It provides the same functionality as the Postman desktop and can be integrated into the CI/CD of the workflow.

# install NPM install -g Newman # Run file Newman run mycollection.json # Run URL Newman run https://www.postman.com/collections/cb208e7e64056f5294e5 -e dev_environment.jsonCopy the code

Viii. Paid features

In addition, Postman provides many paid functions for team collaboration, such as documentation, monitoring, and health checks.

Copyright statement

All the original articles in this blog are reserved by the author. Reprint must contain this statement, keep this article complete, and in the form of hyperlink to indicate the author and the original address of this article: blog.mazey.net/1878.html