Add Header to Every Request in Postman – 在Postman中为每个请求添加标题

最后修改: 2022年 5月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll learn how to add HTTP headers to every request in Postman by using pre-request scripts.

在本教程中,我们将学习如何通过使用预请求脚本为Postman中的每个请求添加HTTP头信息。

2. HTTP Headers

2.HTTP头文件

Before diving into the implementation, let’s review what HTTP headers are.

在深入实施之前,让我们回顾一下什么是HTTP headers

In HTTP requests, the headers are fields that provide additional information between a client and server HTTP communication. HTTP headers have a key-value pair format, and they can be attached to both a request and a response.

在HTTP请求中,头信息是在客户端和服务器HTTP通信之间提供额外信息的字段。HTTP标头有一个键值对的格式,它们可以附加到请求和响应中。

Authorization, content type, and cookies are examples of metadata that can be provided by HTTP headers.

授权、内容类型和cookies是可以由HTTP标头提供的元数据的例子。

For example:

比如说。

Authorization: Bearer YmFyIiwiaWF0IjoxN;
Content-Type: application/json;
Cookie: foo=bar;

We’ll use Postman pre-request scripts functionality to set headers by executing JavaScript code.

我们将使用Postman预请求脚本功能,通过执行JavaScript代码来设置头文件。

3. Running the Server

3.运行服务器

In this tutorial, we’ll use a previous Baeldung project, spring-boot-json, for demonstration purposes. The application consists of a single controller, StudentController, that accepts CRUD operations on a Student java model.

在本教程中,我们将使用之前的Baeldung项目,spring-boot-json,作为示范。该应用由一个控制器StudentController组成,它接受对Studentjava模型的CRUD操作。

We must install all dependencies using the Maven install command, and then we run the SpringBootStudentsApplication file, which will start the Tomcat server on port 8080.

我们必须使用Maven install 命令安装所有依赖项,然后运行SpringBootStudentsApplication 文件,这将在8080端口启动Tomcat服务器。

Using Postman, we can confirm the server is running by sending a GET request to the following endpoint and expect a JSON response:

使用Postman,我们可以通过向以下端点发送GET请求来确认服务器正在运行,并期待一个JSON响应。

http://localhost:8080/students/

For example:

比如说。

Initial GET Request

Now that we verified the server is running, we can programmatically add HTTP headers to our requests sent by Postman.

现在我们验证了服务器正在运行,我们可以以编程方式向Postman发送的请求添加HTTP头信息

4. Add Header with Pre-request Script

4.用预请求脚本添加页眉

To add headers to an HTTP request in Postman with pre-request scripts, we need to access the request data provided by the Postman JavaScript API object named pm.

要在Postman中用预请求脚本为HTTP请求添加头信息,我们需要访问Postman JavaScript API对象提供的请求数据,该对象名为pm.

We can perform operations on the request metadata by calling the pm.request object; therefore, we can add, modify and delete HTTP headers prior to sending a request.

我们可以通过调用pm.request 对象对请求元数据进行操作;因此,我们可以在发送请求之前添加、修改和删除HTTP头。

As discussed earlier, HTTP headers have a key-value pair format. The Postman JavaScript API expects both a key and a value to be provided when adding headers to the request.

正如前面所讨论的,HTTP头文件有一个键值对的格式。Postman JavaScript API希望在向请求添加头信息时,同时提供一个键和一个值。

We can add a header by using the name: value format as a string:

我们可以通过使用name: value 格式的字符串来添加一个头。

pm.request.headers.add("foo: bar");

We can also pass a JavaScript object with the key and value properties as follows:

我们也可以传递一个带有key value 属性的JavaScript对象,如下所示。

pm.request.headers.add({
  key: "foo",
  value: "bar"
});

However, per Postman documentation, we could add additional properties to the header object, such as id, name, and disabled, which will extend the functionality within the Postman JavaScript runtime environment.

然而,根据Postman文档,我们可以向头对象添加额外的属性,如idnamedisabled,这将在Postman JavaScript运行环境中扩展功能。

Now, let’s see this in action. First, we’ll add a script to an individual Postman request; then, we’ll add headers for an entire collection.

现在,让我们看看这个动作。首先,我们将为一个单独的Postman请求添加一个脚本;然后,我们将为整个集合添加头文件。

4.1. Individual Request

4.1.个人请求

We can add headers to individual requests in Postman by using pre-request scripts. We can refer to the implementations shown in the previous section; however, we’ll focus on the second one, where we pass a JavaScript object so that we can add additional properties that extend the functionality.

我们可以通过使用预请求脚本在Postman中为单个请求添加头信息。我们可以参考上一节所示的实现;然而,我们将重点关注第二个实现,即我们传递一个JavaScript对象,这样我们就可以添加扩展功能的额外属性。

In the pre-request Script on the Postman window, we add the following script indicating that the client expects a response of type json:

在Postman窗口的pre-request Script中,我们添加以下脚本,表明客户端期望得到一个json类型的响应。

pm.request.headers.add({
    key: "Accept",
    value: "application/json"
});

In Postman, the request looks as follows:

在Postman中,该请求看起来如下。

Individual Pre request script

Now, we send a GET request by clicking on the Send button. Once the request is sent, we must open the Postman console (normally by clicking on the console button on the bottom left corner) and expand our most recent request to see the Request Headers section:

现在,我们通过点击Send按钮发送一个GET请求。一旦请求被发送,我们必须打开Postman控制台(通常通过点击左下角的console按钮),并展开我们最近的请求,以看到Request Headers部分。

Individual Request Console

In the console, we can see the Accept: “application/json” header indicating that it was successfully attached to the GET request by the script. Additionally, we can check the body and status code of the response to confirm the request was successful.

在控制台,我们可以看到Accept:”application/json”头,表明它被脚本成功地附加到GET请求中。此外,我们可以检查响应的主体和状态代码,以确认请求是否成功。

To further verify the pre-request script, we could add the following header and expect an empty response along with a status code of 406 Not Acceptable:

为了进一步验证预请求脚本,我们可以添加以下标头,并期待一个空的响应,以及一个状态代码为406 Not Acceptable

pm.request.headers.add({ 
    key: "Accept",
    value: "image/*" 
});

4.2. Collection

4.2.采集

Similarly, we can add HTTP headers to entire collections with pre-request scripts.

类似地,我们可以通过预请求脚本为整个集合添加HTTP头信息。

First, we’ll create a Student API Collection to test our API endpoints with Postman and confirm that every request contains the headers we add with the Pre-request script.

首先,我们将创建一个学生API集合,以用Postman测试我们的API端点,并确认每个请求都包含我们用Pre-request脚本添加的头文件。

In Postman, we can group web API endpoints by going to the Collections menu option on the left. Then, we click the plus sign button and create a new collection named Student API Collection:

在Postman中,我们可以通过进入左边的Collections菜单选项来分组网络API端点。然后,我们点击加号按钮,创建一个名为Student API Collection:的新集合。

Student API Collection

Note that we also added two endpoints to our collection: http://localhost:8080/students/ and http://localhost:8080/students/2.

请注意,我们还向我们的集合添加了两个端点。http://localhost:8080/students/ http://localhost:8080/students/2

Similar to individual requests, we can add pre-request scripts to our collection by selecting Student API Collection on the left menu and going to the Pre-request Script tab. Now, we can add our script:

与单个请求类似,我们可以通过选择左侧菜单上的Student API Collection,并进入Pre-request Script标签,将预请求脚本添加到我们的集合中。现在,我们可以添加我们的脚本。

pm.request.headers.add({ 
    key: "Accept",
    value: "application/json" 
});

In Postman, the Student API Collection should look like this:

在Postman中,Student API Collection应该看起来像这样。

Before running the collection, we must make sure that we removed the pre-request scripts we initially added in the previous section. Otherwise, the HTTP headers will be overridden by those specified in the request script, and those at the collection level will be dismissed.

在运行集合之前,我们必须确保删除我们最初在上一节中添加的预请求脚本。否则,HTTP头信息将被请求脚本中指定的头信息所覆盖,而在集合层面的头信息将被驳回。

Now, we are ready to run our collection. Hit the Run button on the collection bar, and the Runner tab will open automatically:

现在,我们已经准备好运行我们的集合。点击集合栏上的Run按钮,Runner标签将自动打开。

Student API Collection Runner

The Runner tab allows us to order our requests, select or deselect requests from our collection, and specify additional settings. Click on Run Student API Collection to execute our requests.

Runner标签允许我们对我们的请求进行排序,从我们的集合中选择或取消选择请求,并指定其他设置。点击运行学生API集合以执行我们的请求。

Once the entire collection is finished, we can see the order of executions and test results, if any. However, we want to make sure our HTTP headers were part of our requests, and we can confirm that by opening the Postman console:

一旦整个集合完成,我们可以看到执行的顺序和测试结果,如果有的话。然而,我们要确保我们的HTTP头是我们请求的一部分,我们可以通过打开Postman控制台来确认这一点。

Student API Collection Console

Once again, we can expand the Request Headers section of our requests in the console and confirm that our pre-request script added the Accept header. Additionally, you can confirm the requests were successful by looking at the status code and response body.

再次,我们可以在控制台中展开请求的Request Headers 部分,确认我们的预请求脚本添加了Accept 头。此外,你可以通过查看状态代码和响应体来确认请求是否成功。

5. Conclusion

5.总结

In this article, we have used the pre-request Script functionality of Postman to add HTTP headers to every request. First, we reviewed what HTTP headers are; then we added pre-request scripts to both individual requests and collections in order to add headers. Refer to the Postman documentation for further exploration of pre-request scripts and other features.

在这篇文章中,我们使用了Postman的预请求脚本功能来为每个请求添加HTTP头信息。首先,我们回顾了什么是HTTP头信息;然后,我们在单个请求和集合中都添加了预请求脚本,以便添加头信息。请参考Postman文档以进一步了解预请求脚本和其他功能。

The collection created in this tutorial is available over on GitHub.

在本教程中创建的集合可在GitHub上获得