eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

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

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

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:

Initial GET Request

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:

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:

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.

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:

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:

Student API Collection with Script

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:

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.

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:

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.

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 code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

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

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (All)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)