How to Use JMeter's HTTP Header Manager?
Last Updated :
15 Jul, 2024
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:
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".
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".
Open JMeter & Create a Test PlanRight-click on the Thread Group, then navigate to "Add" > "Config Element" > "HTTP Header Manager".
Add an HTTP Header Manager 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.
Configure Default HeadersName: 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"));
For more granular control, you can add headers to individual HTTP requests. This is useful when different requests require different headers.
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".
Add an HTTP RequestRight-click on the HTTP Request, then navigate to "Add" > "Config Element" > "HTTP Header Manager". This Header Manager will only apply to this specific request.
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);
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.
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.
create a new test plan by right-clicking on the Test Plan example node and selecting "Add" > "Threads (Users)" > "Thread Group".
Follow Steps to Add Default HeadersStep 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.
Add a Specific HTTP Request and its Own HTTP Header ManagerIn 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:
Override the Default HeadersName: 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
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
Authorization: Bearer <your-token>
Content-Type: application/xml
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.