In this tutorial, we'll learn how to add HTTP headers to every request in Postman by using pre-request scripts.
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.
Authorization: Bearer YmFyIiwiaWF0IjoxN;
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:
Now that we verified the server is running, we can programmatically add HTTP headers to our requests sent by Postman.
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.
We can add a header by using the name: value format as a string:
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
In the pre-request Script on the Postman window, we add the following script indicating that the client expects a response of type 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:
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:
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.
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.
Course – LS (cat=HTTP Client-Side) res – HTTP Client (eBook) (cat=Http Client-Side)