Open In App

How to Use JMeter's HTTP Header Manager?

Last Updated : 15 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Apache JMeter is a famous open-source tool used to load-test functional behavior and measure performance. So today we are going to see how HTTP Header Manager can be so useful, its functionality is directly related to the management of headers on your HTTP Request. Needless to say, for sending the information about request (which could include things like content type of body), but also headers go into user agent data as well as credentials. Properly configuring HTTP headers can completely impact the behavior and performance of your web tests. In this article, we'll explore how to use JMeter's HTTP Header Manager.

These are the following usage of JMeter's HTTP Header Manager:

Adding Default Headers

You can configure default headers that apply to all HTTP requests within a test plan. This approach is useful for setting common headers like "User-Agent" or "Content-Type".

Steps to add Default Headers

Adding default headers in JMeter ensures that certain headers are included in all HTTP requests within a test plan. This is particularly useful for headers that are required consistently, such as "User-Agent" to mimic various browsers or "Content-Type" to specify the data format being used. Setting these headers at the HTTP Header Manager level ensures uniformity across all requests, reducing the need to add them individually each time.

Step 1: Open JMeter & Create a Test Plan

Launch JMeter and create a new test plan by right-clicking on the Test Plan example node and selecting "Add" > "Threads (Users)" > "Thread Group".

2
Open JMeter & Create a Test Plan

Step 2: Add an HTTP Header Manager

Right-click on the Thread Group, then navigate to "Add" > "Config Element" > "HTTP Header Manager".

3
Add an HTTP Header Manager

Step 3: Configure Default Headers

In the HTTP Header Manager, click "Add" to include new headers. For instance, you can set "User-Agent" to simulate different browsers or "Content-Type" for API requests.

4
Configure Default Headers

Name: User-Agent

Value: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Example Code (Note: This is example code only for understanding and the name-value can be added manually as shown in the above image)

// Set default headers in HTTP Header Manager
headerManager.add(new Header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/91.0.4472.124 Safari/537.36"));
headerManager.add(new Header("Content-Type", "application/json"));

Adding Headers to Specific Requests

For more granular control, you can add headers to individual HTTP requests. This is useful when different requests require different headers.

Steps for Adding Headers to Specific Requests

In JMeter, you can also add headers to individual HTTP requests, which provides greater flexibility and control over your test scenarios. This is particularly useful when certain requests require unique headers, such as different authorization tokens or specific content types. By attaching an HTTP Header Manager to specific requests, you can customize the headers for each request as needed.

Step 1: Add an HTTP Request

Right-click on the Thread Group, navigate to "Add" > "Sampler" > "HTTP Request".

5
Add an HTTP Request

Step 2: Add an HTTP Header Manager to the HTTP Request

Right-click on the HTTP Request, then navigate to "Add" > "Config Element" > "HTTP Header Manager". This Header Manager will only apply to this specific request.

6

Step 3: Configure Headers

In the HTTP Header Manager, click "Add" to include new headers. For instance, you can set "Authorization" to simulate different browsers or "Content-Type" for API requests. These headers will override the default headers for this particular request.

Name: Authorization

Value: Bearer <your-token>

Example Code: (Note: This is example code only for understanding and the name-value can be added manually as shown in the above image)


// Add headers to a specific HTTP request
HttpSamplerProxy httpSampler = new HttpSamplerProxy();
headerManager.add(new Header("Authorization", "Bearer <your-token>"));
headerManager.add(new Header("Content-Type", "application/xml"));
httpSampler.setProperty(HTTPHeaderManager.HEADER_MANAGER, headerManager);

Adding Overriding Headers

In few cases, you probably need to override default headers which are set by the HTTP Header Manager for specific requests. This approach helps in scenarios where certain requests are require unique header values.

Steps to add Overriding Headers

By overriding headers, you can ensure that your performance tests accurately reflect the conditions your application will face in production. This capability is particularly useful for testing how your application handles different client types, geographic locations, and other variables that can be specified through HTTP headers.

Step 1: Follow Steps to Add Default Headers (Creating Thread Group)

create a new test plan by right-clicking on the Test Plan example node and selecting "Add" > "Threads (Users)" > "Thread Group".

8
Follow Steps to Add Default Headers

Step 2: Add a Specific HTTP Request and its Own HTTP Header Manager

Right-click on the HTTP Request, then navigate to "Add" > "Config Element" > "HTTP Header Manager". This Header Manager will only apply to this specific request.

9
Add a Specific HTTP Request and its Own HTTP Header Manager

Step 3: Override the Default Headers

In the HTTP Header Manager for the specific request, add the headers that need to be overridden. For example, if the default "User-Agent" is set to Chrome, but you want this request to simulate a Safari browser:

10
Override the Default Headers

Name: User-Agent

Value: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15

Example:

// Override default headers for a specific HTTP request
HttpSamplerProxy specificHttpSampler = new HttpSamplerProxy();
HeaderManager specificHeaderManager = new HeaderManager();
specificHeaderManager.add(new Header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
AppleWebKit/605.1.15 (KHTML, like Gecko)
Version/14.0.3 Safari/605.1.15"));
specificHttpSampler.setProperty(HTTPHeaderManager.HEADER_MANAGER, specificHeaderManager);

Note: This is example code only for understanding and the name-value can be added manually as shown in the above image

Used Configuration

Note: Following are the configurations used earlier to configure Apache JMeter

Adding Default Headers

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
Content-Type: application/json

Adding Headers to Specific Requests

Authorization: Bearer <your-token>
Content-Type: application/xml

Overriding Headers

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15

Conclusion

The proper utilization of JMeter HTTP Header Manager has the potential to drastically boost the precision and productivity levels in your web application tests. Whether you are defining default headers, including custom ones for selected requests or performing header overrides - JMeter offers a highly modular and versatile interface to work with HTTP Headers. When you know these methods and start using them correctly, your tests will be comprehensive enough to simulate real world scenarios which pave the way towards more robust web applications.


Next Article
Article Tags :

Similar Reads