The JMeter HTTP Request: How and When to Use It
One of the most used samplers when configuring a test plan in JMeter is the HTTP Request. This blog post will explain how to configure it, best practices on how to use it, and will show an example of making a post request with this sampler and seeing the results with a listener element.
What is the JMeter HTTP Request?
A sampler that lets you send an HTTP/HTTPS request to a web server for load testing. There are different methods the sampler is able to use, like:
How to Configure and Use the JMeter HTTP Request
To add an HTTP Request, add a Thread Group, and then right-click on Thread Group -> Add -> Sampler -> HTTP Request (see figure 1).
Figure 1 - Adding an HTTP Request sampler
Now we get to the awesome part, the configuration. In the first section of the sampler, you will find two text fields:
- One sets the name of the label.
- The other is for adding a comment, if you think it’s necessary.
A best practice is to set a descriptive name of the label to make it easy to check it when executing the test.
Figure 2 - Setting a descriptive label
To set up a basic request you need to specify the protocol (HTTP or HTTPS), server name or IP (Example: blazerunner.io), port, method (Example: GET), and the path, if the request has one (see figure 3).
Figure 3 - Configuration with mandatory fields to reach the server.
There are additional options that we can configure when we want to make an HTTP request:
- Redirect Automatically: This option will check if the response of the server is a redirection. If this is true, it will redirect to this URL.
- Follow redirects: This option will also check if the response of the server is a redirection. If this is true, it will also redirect to the URL. The difference from the first redirect (Redirect Automatically) is that Redirect Automatically will not consider each redirect as a separate request, whie Follow Redirects does. This will be seen when you add a listener, like the View Results Tree.
- Use KeepAlive: Sets the connection value to KeepAlive as the request header.
- Use multipart/form-data: Sets the Content-type value to multipart/form-data as the request header.
- Browser-compatible headers: When using multipart/form-data, this option suppresses the Content-Type and Content-Transfer-Encoding headers; so only the Content-Disposition header is sent.
Picture 4 - Options to add to the HTTP request
The last component section is where you can set parameters to make the request. There are three options here:
- The first two, “Parameters” and “Body data” enable sending request information to the server.
- “File upload” is for uploading files, like images, video, etc.
Figure 5 - Setting parameters to the request
If you want to set up an advanced config you need to switch to the Advanced tab and you will see some other options. Let's go through a quick overview of them.
The Client implementation option is the HTTP protocol implementation to be used. The recommended option is using HTTPClient4, and it’s even set by default so you don't even have to bother about it. But you can also use the Java implementation if you need to.
If you want to set a connection and response timeout, set the Timeouts (milliseconds) option. If you execute an HTTP request and the response or connection time of the server is higher than the one you specified, the HTTP request will be stopped.
Figure 6 - Client implementation and Timeouts (milliseconds)
Retrieving Embedded Resources
Figure 7 - Embedded resources from HTML files
Source and Proxy
Now let's look at the option Source address. As you can tell from the name, is a feature to send requests from a specific IP address.
The Proxy server option is another useful feature when you need to test a site that uses a proxy. You need to specify server name or IP, username and password.
Figure 8 - Source address and Proxy server
Best Practices for using the HTTP request
- Use short and meaningful names to identify the request during an execution.
- Use HTTP request defaults. This config element is used when all requests in the JMeter script are sent to the same server. This way, when you create a new HTTP request you only need to specify the method, path you want to reach and parameters if it’s needed.
- Add an HTTP Cookie manager to the root of the test plan. This config element handles the cookies the server needs.
- Add assertions to the HTTP request to endure you reach the correct site.
Creating a Post with the HTTP request
In this example, I want to search the available flights from Paris to Buenos Aires. I will:
- add an HTTP request
- set Search Flights as the label name
- HTTPS is the protocol
- the server name or IP is blazedemo.com
- the method is a POST
- the path is /reserve.php
- the body of the POST contains two variables with their respective values. Variable one is fromPort, with the value is Paris, and the variable two is toPort and the value is Buenos+Aires (The + symbol due to the blank space is encoded).
Lets see what it looks like configured in JMeter and the result of the execution:
Figure 9 - Post configuration to get an available flight from Paris to Buenos Aires
Figure 10 - The result of the execution of the HTTP request named “Search Flights”. To see the result in a friendly way I use the option Browser. You can check it in the upper right corner.
That’s it! Now you have seen how to configure an HTTP request in JMeter, If you haven’t used it yet, I highly encourage you to give it a try! When you’re done, run your test in BlazeMeter for scalability, advanced monitoring and continuous testing.