Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Postman

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use Postman, the introduction in the article is very detailed, has a certain reference value, interested friends must read!

What is Postman (past life and present life)

Postman was born in 2013 and started as a tool for Abhinav Asthana to solve API testing. With the rapid growth of users and demand for this tool, Abhinav Asthana found two of his former colleagues, Ankit Sobti and Abhijit Kane, to create the company Postman Inc.

Today, Postman has become a collaborative platform for API development. Postman simplifies every step of building an API and simplifies collaboration so that an API can be created more quickly.

Second, use variables

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

For example, set the domain name address as a variable in different running 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, the name in Environment will be taken.

2.1 variable scope is suitable for different scenarios in Postman

Global: global variables can be used throughout the workspace (Workspace) because they are uncontrollable and confusing. They should be immutable global constants and should be used with caution.

Pm.globals.set ("variable_key", "variable_value"); pm.globals.get ("variable_key")

Collection: collection variables are available in a single collection (Collection) and often have common business binding attributes, such as commodity attributes, membership levels, general keys, and so on.

Pm.collectionVariables.set ("variable_key", "variable_value"); pm.collectionVariables.get ("variable_key")

Environment: environment variables allow requests to adapt to different environments, such as local, test, rehearsal, and production environments, often used to distinguish request addresses.

Pm.environment.set ("variable_key", "variable_value"); pm.environment.get ("variable_key")

Data: data variables come from external CSV and JSON files and are only used when running through Newman or Runner.

Local: local variables are only available in the lifecycle of a single request and are automatically destroyed after running.

Pm.variables.set ("variable_key", "variable_value"); pm.variables.get ("variable_key")

2.2 Editing global and environment variables

2.3 Editing collection variables

2.4 use system built-in dynamic variables

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

Note: Postman supports printing debug information in Pre-request Script and Tests, which is consistent with the browser console. You can use commands: console.log (), console.info (), console.warn (), and console.error ().

Console.log ('current timestamp:', pm.variables.replaceIn ('{{$timestamp}}'); console.log ('random color:', pm.variables.replaceIn ('{{$randomColor}}')); console.log ('random IP:', pm.variables.replaceIn (' {{$randomIP}')); console.log ('random name:', pm.variables.replaceIn ('{{$randomFullName}}')) Console.log ('random occupation:', pm.variables.replaceIn ('{{$randomJobType}}'); console.log ('random city:', pm.variables.replaceIn ('{{$randomCity}}')); console.log ('random picture:', pm.variables.replaceIn ('{{$randomImageUrl}}')) # output current timestamp: 1609060090 Random Color: azure Random IP:163.140.207.64 Random name: Chester Funk Random occupation: Coordinator Random City: Port Devinborough Random Picture: http://placeimg.com/640/480 III, Postman request Life cycle

In Postman, a complete Postman request life cycle includes pre-request scripts (pre-request script) and post-test scripts (tests script) in addition to regular requests (request) and responses (response). Postman includes a powerful Node.js-based runtime (runtime) that allows users to write JavaScript code in pre-request script and tests events.

3.1 use scripts in pre-request (pre-request script)

A pre-request script (pre-request script), as its name implies, is a script that executes before a request is sent.

3.2 send request (request)

Tip 1: use: id Custom path parameters in links

Tip 2: Cookie is editable

3.3 receive a response (response)

Tip: save the response result

The saved result can be used as a case or record for development.

3.4 use scripts in testing (tests)

Postman supports testing scripts to verify that the request meets expectations after the request has responded.

Example 1: verify that the response status code is 200

Pm.test ("Status test", function () {pm.response.to.have.status;})

Example 2: verify whether the returned business data (JSON) meets expectations

Pm.test ("request successful!" , function () {var jsonData = pm.response.json (); pm.expect (jsonData.message) .to.eql ('success');}); fourth, use Postman to grab packages

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

The Postman application listens for any calls made by the client application or device.

The Postman agent captures the request and forwards it to the server.

The server returns the response to the client through the Postman agent.

4.1 Open grab bag

Native IP address:

Mobile phone settings:

4.2 bag grabbing effect

5. Use agents

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

In addition to conveying information, agents can do more:

Record all traffic between your machine and the Internet. Displays the contents of all requests, responses, Cookie, and headers. Routes traffic to a specified Internet location. Debug interface. Prevent direct attacks and ensure security. DevOps load balancing.

By default, Postman will use its own system agent, and if you customize an agent, it will take precedence over its own system agent.

VI. Use Collection Runner

The collection operator (Collection Runner) allows requests within the collection to be run in a specified order. Collection Runner records the test results of the request, and the script can pass data between requests.

Command line scaffolding Newman

Postman provides the scaffolding tool Newman to run Collection requests on the command line, which 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 URLnewman run https://www.postman.com/collections/cb208e7e64056f5294e5-e dev_environment.json

VIII. Payment function

In addition, Postman provides a lot of paid functions for team collaboration, such as documentation, monitoring, health check and so on.

The above is all the contents of this article "how to use Postman". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report