1. Overview
1.概述
Postman is a popular API development tool that simplifies designing, testing, modifying, and documenting APIs. It provides a user-friendly interface allowing users to send and receive HTTP requests, manage workflows with environments and collections, perform automated testing, create mock servers for testing, and generate API documentation.
Postman 是一种流行的 API 开发工具,可简化 API 的设计、测试、修改和文档编制。它提供了友好的用户界面,允许用户发送和接收 HTTP 请求、使用环境和集合管理工作流、执行自动测试、创建用于测试的模拟服务器并生成 API 文档。
Due to its versatility, it’s highly valued by developers, testers, and other IT professionals engaged in API-centric workflows.
由于其多功能性,开发人员、测试人员和其他从事以应用程序接口为中心的工作流程的 IT 专业人员都对它给予了高度评价。
In this tutorial, we’ll discuss how to install, setup, and use the most important features of Postman.
在本教程中,我们将讨论如何安装、设置和使用 Postman 的最重要功能。
2. Installation and Setup
2.安装和设置
Postman is available for download as a desktop application for Windows, Mac, or Linux operating systems. It’s also available as a web application. However, not all features are available there.
Postman 可作为桌面应用程序下载,适用于 Windows、Mac 或 Linux 操作系统。它也可以作为网络应用提供。不过,并非所有功能都可以在这里使用。
After installation, we have to sign up and create a free account.
安装完成后,我们必须注册并创建一个免费账户。
3. Sending an HTTP Request
3.发送 HTTP 请求
One of the fundamental actions in Postman is sending a request to an API. Postman provides a collection of free public REST APIs that don’t require any authentication, and we’ll utilize those APIs to demonstrate some of Postman’s functionalities.
Postman的基本操作之一是向API发送请求。Postman提供了一系列无需任何身份验证的免费公共REST API,我们将利用这些API来演示Postman的部分功能。
We’ll use the API to retrieve colleges and universities in a specific country.
我们将使用 API 来检索特定国家的高校。
Let’s create a workspace named “baeldung-test-workspace”. Once created, we can click the New button and select HTTP.
让我们创建一个名为“baeldung-test-workspace “的工作区。创建完成后,我们可以单击新建按钮并选择HTTP。
To test sending a request in Postman, we’ll set the URL to http://universities.hipolabs.com/search?country=Germany and the method to GET:
为了测试在 Postman 中发送请求,我们将 URL 设置为 http://universities.hipolabs.com/search?country=Germany,方法设置为 GET:
After sending the request, Postman will display the response data in the sidebar. We can view the status code, headers, and response body here.
发送请求后,Postman 会在侧边栏显示响应数据。我们可以在这里查看状态代码、标题和响应正文。
If we don’t receive a successful response, the response status and details in the sidebar will help identify the issue.
如果我们没有收到成功的回复,侧边栏中的回复状态和详细信息将有助于确定问题所在。
In this example, we are using the GET method, but depending on the scenario, we can choose other most common methods such as POST, PUT, PATCH, DELETE, or input a new method.
在本示例中,我们使用的是 GET 方法,但根据不同的场景,我们可以选择其他最常用的方法,如 POST、PUT、PATCH、DELETE 或输入新方法。
We also used the query parameter for the country name. When using the query parameters, they can be entered either in the request URL or in the dedicated Query params tab, where they can be entered as key-value pairs. Postman naturally supports path parameters, which are also entered as a part of the request URL.
我们还为国家名称使用了查询参数。使用查询参数时,可以在请求 URL 中输入,也可以在专用的查询参数选项卡中输入。Postman 自然也支持路径参数,这些参数也可以作为请求 URL 的一部分输入。
4. Environments
4.环境
Effectively managing requests across various environments can be complex. However, Postman’s environments allow us to manage variables for different environments, making switching between, for example, development, testing, and production easy.
有效管理不同环境下的请求可能很复杂。不过,Postman 的环境允许我们管理不同环境的变量,从而使开发、测试和生产等环境之间的切换变得简单。
Let’s create two different environments by clicking the New button and selecting Environment: development and test:
让我们通过单击 New 按钮并选择 环境:开发和测试来创建两个不同的环境:
Here, we can enter both initial and current values. The initial value is the default setting for a variable, while the current value is used in the actual requests when the variable is referenced. The initial value is used as the current value in case we leave the current value blank when saving the environment.
在这里,我们可以输入初始值和当前值。初始值是变量的默认设置,而当前值则在实际请求中引用变量时使用。如果我们在保存环境时将当前值留空,初始值将被用作当前值。
We’ll create a variable country in each, assigning the value “Germany” for development and “France” for the test environment:
我们将在每个变量中创建一个变量 country,为开发环境指定值为 “德国”,为测试环境指定值为 “法国”:
We selected the Development environment in the upper right corner and clicked the Send button. The country’s value was resolved to “Germany”. The variables are not reserved only for the query parameters – they can be referenced in the request URLs, headers, and body data.
我们选择了右上角的开发环境,并点击了发送按钮。国家的值被解析为 “德国”。变量并非只保留给查询参数,它们可以在请求 URL、标题和正文数据中引用。
5. Collections
5.收藏品
Managing individual requests for various aspects of the API can be a challenge in terms of effective organization. Additionally, APIs with parameters that differ across different environments require executing the same set of requests in different environments without modifications.
在有效组织方面,管理 API 各方面的单个请求是一项挑战。此外,不同环境下参数不同的应用程序接口需要在不同环境下执行相同的请求集,而无需进行修改。
This is where collections prove helpful, as they help manage API workflows more efficiently. A collection can be related to a specific environment, allowing us to use environment variables in requests.
集合可以与特定环境相关联,从而允许我们在请求中使用环境变量。
In API workflows, executing multiple requests in a specific order is often necessary, and collections can help by executing requests in a particular order.
在 API 工作流中,通常需要按照特定顺序执行多个请求,而 集合可通过按照特定顺序执行请求来提供帮助。
In general, collections are an effective way to group requests for easier management and collaboration among team members.
一般来说,集合是一种对请求进行分组的有效方式,便于团队成员之间的管理和协作。
5.1. Creating a Collection
5.1.创建收藏集
Let’s create a new collection by clicking the Create Collection button or saving our existing GET request. We’ll also add a GET request to retrieve all colleges and universities in France and save it to this new collection.
让我们点击 Create Collection 按钮或保存现有的 GET 请求,创建一个新的集合。我们还将添加一个 GET 请求,以检索法国的所有学院和大学,并将其保存到这个新集合中。
By clicking on the collection’s name in the left sidebar, we’re presented with multiple tabs, each allowing us to customize and configure our collection, such as by adding tests that will be executed with each request in the collection.
点击左侧边栏中的集合名称,我们将看到多个选项卡,每个选项卡都允许我们自定义和配置我们的集合,例如添加将在集合中的每个请求中执行的测试。
5.2. Collection Runner
5.2.收集转轮
We’ll now focus on running our collection by utilizing the Collection Runner, where we can run our requests in a specific order, either manually, on a schedule, or by running it from the command line. We can test the functional aspect of our API this way. Still, the Collection Runner also allows us to test the performance of our API by simulating a real-world traffic scenario.
现在,我们将重点利用 Collection Runner 来运行我们的集合,在这里,我们可以按照特定顺序运行我们的请求,既可以手动运行,也可以按计划运行,还可以通过命令行运行。我们可以通过这种方式测试 API 的功能方面。此外,Collection Runner 还允许我们通过模拟真实世界的流量场景来测试 API 的性能。
Collection Runner can be further configured in various ways, but we’ll keep things simple for demonstration purposes and only select the manual run option.
Collection Runner 可以通过多种方式进一步配置,但为了演示的目的,我们将保持简单,只选择手动运行选项。
Finally, the collection can be run by clicking the Run Test collection button:
最后,点击运行测试集合按钮即可运行集合:
Now, we can review the results of our collection run. Postman will display the results of executed requests and eventual test results if any tests were included. Here, we can view helpful information such as environment, duration, average response time, and results for each request.
如果包含任何测试,Postman 将显示已执行请求的结果和最终测试结果。在这里,我们可以查看每个请求的环境、持续时间、平均响应时间和结果等有用信息。
6. Summary
6.总结
In this article, we discussed Postman – a powerful tool for working with APIs.
在这篇文章中,我们讨论了 Postman–一种用于处理 API 的强大工具。
We covered the essentials of how to send an HTTP request, group variables into environments that can be referenced when sending requests, and group requests into collections. However, this only scratches the surface of Postman’s extensive features. Advanced features, such as automated testing through collections and creating mock servers for testing, can significantly enhance our API development process, especially when dealing with complex scenarios.
我们介绍了如何发送 HTTP 请求、将变量分组到可在发送请求时引用的环境中以及将请求分组到集合中等基本功能。高级功能(例如通过集合进行自动测试和创建用于测试的模拟服务器)可显著增强我们的 API 开发流程,尤其是在处理复杂场景时。