HTTP Client Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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

2. HTTP Headers

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

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.

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

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.

3. Running the Server

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.

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.

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

http://localhost:8080/students/

For example:

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

4. Add Header with Pre-request Script

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.

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.

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.

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

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

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

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.

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.

4.1. Individual Request

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.

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

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

In Postman, the request looks as follows:

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:

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.

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:

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

4.2. Collection

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

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.

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:

Note that we also added two endpoints to our collection: http://localhost:8080/students/ and 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:

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

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

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.

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

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.

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:

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.

5. Conclusion

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.

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

HTTP Client bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
HTTPClient footer
guest
0 Comments
Inline Feedbacks
View all comments