The JMeter HTTP Request: How and When to Use It
February 15, 2021

The JMeter HTTP Request: How and When to Use It

Open Source Automation

One of the most used samplers in JMeter when configuring a test plan is the JMeter HTTP Request. This blog post will explain how to configure a JMeter HTTP Request and explore best practices on how to use it. 

This blog will also show an example of making a post request with this sampler and seeing the results with a listener element. 

Table of Contents:

What is the JMeter HTTP Request?

The JMeter HTTP Request is 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:

  • GET
  • POST
  • PUT
  • HEAD
  • DELETE
  • PATCH
  • OPTIONS

Take JMeter to the next level with BlazeMeter load testing.

Start Free Trial

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). 

adding an HTTP request

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:

  1. One sets the name of the label.
  2. The other is for adding a comment, if you think it’s necessary.

A best practice is to set a descriptive name for the label to make it easy to check it when executing the test.

setting a descriptive name

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).

Configuration with mandatory fields to reach the server.

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.
Options to add to the HTTP request

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.
Setting parameters to the request

Figure 5 - Setting parameters to the request

Advanced Configurations

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.

Client Implementation

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. 

Timeout

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.

Client implementation and Timeouts (milliseconds)

Figure 6 - Client implementation and Timeouts (milliseconds)

Retrieving Embedded Resources

An additional ability is to retrieve all the embedded resources from HTML files. Why? When requesting a URL for a web page, the HTML resources can have different static resources, like images used in the page, JavaScripts that must execute certain code on the client-side, and CSS files for adding style to our HTML elements. After receiving the request, the browser gets the HTML, parses it, and retrieves all the embedded resources automatically with new HTTP requests. To make JMeter behave more like a browser it is recommended to enable the Retrieve All Embedded Resources option.

Embedded resources from HTML files

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.

Source address and Proxy server

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:

null

Figure 9 - Post configuration to get an available flight from Paris to Buenos Aires

null

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.

START TESTING NOW