HTTP Request Sampler in Jmeter with Examples

If you’ve just started with Apache JMeter, one of the most essential components you’ll come across is the HTTP Request Sampler. It’s the workhorse when it comes to testing the performance of web applications, APIs, and services over HTTP.

In this guide, we’ll break down what the HTTP Request Sampler is, how to use it, and provide a hands-on example to help you get started – even if you’ve never used JMeter before.

The HTTP Request Sampler is a crucial component in JMeter used to send HTTP/HTTPS requests to a server. It allows you to configure various request parameters, such as the URL, HTTP method (e.g., GET, POST), request headers, and body data. When executed, this sampler sends the request to the specified server and records the response, which aids in analyzing the server’s behavior under test conditions.

You should use the HTTP Request Sampler when:

  • Load testing REST APIs
  • Verifying that a service or endpoint responds correctly under load
  • Measuring response times, errors, and throughput

Let’s go step-by-step:

  • Open JMeter and create a new Test Plan.
  • Right-click on Test Plan → Add → Threads (Users)Thread Group.
  • Right-click on Thread Group → Add → SamplersHTTP Request.

You’ve just added your first HTTP Request Sampler!

HTTP Request Sampler Page

As you can see, the HTTP Request Sampler has several fields under the Basic Section. Here’s how you can fill them out:

  • Name: Give your sampler a descriptive name, such as “Home Page Request”.
  • Server Name or IP: Enter the server’s domain name or IP address, e.g., jsonplaceholder.typicode.com
  • Protocol: Specify http or https depending on your target URL.
  • Method: Choose from HTTP methods like GET, POST, PUT, DELETE, etc.
  • Path: Define the path to the resource you’re interested in, such as /posts/1
  • Parameters: For requests involving parameters, add them in the ‘Parameters’ section.
  • Body Data: Input any body data if the request requires it (commonly used in POST requests).

As an example, we will try to execute the “https://jsonplaceholder.typicode.com/posts/1” API using JMeter. This API returns a sample blog post in JSON format.

Steps for Configuration:

  1. Name: Get Request
  2. Server Name or IP: jsonplaceholder.typicode.com
  3. Protocol: https
  4. HTTP Method: GET
  5. Path: /posts/1
HTTP Request Sampler Page after filling up the details

Now we’re ready to execute our test, but before that, let’s add the ‘View Results Tree‘ listener to visualize the results.

Adding View Results Tree

Okay, let’s now click on the Green Run Button to run the Test Plan.

Run the Test Plan to demonstrate HTTP Request sampler

As you can see, the GET request was executed once because we set both the Number of Users and the Loop Count to 1 in the Thread Group settings. And that’s how you can easily execute a GET request using the HTTP Request Sampler in JMeter.

Result after executing HTTP Request Sampler
Example: Executing a Simple HTTP POST Request

Earlier, we executed a GET request. Now, let’s try executing a POST request.

As an example, we will send the payload below to the “https://jsonplaceholder.typicode.com/posts” API using JMeter.

{
  "title": "Hello JMeter",
  "body": "This is a test post",
  "userId": 1
}

Steps for Configuration:

  1. Name: Post Request
  2. Server Name or IP: jsonplaceholder.typicode.com
  3. Protocol: https
  4. HTTP Method: POST
  5. Path: /posts
  6. Select Body Data and paste the Request Payload
{
  "title": "Hello JMeter",
  "body": "This is a test post",
  "userId": 1
}
POST Request Execution using HTTP Request Sampler

Click on the Run Button again and observe the results.

Results After executing the POST request sampler

We can see that the payload was sent along with the POST request.

There may be scenarios where we need to send a token or other information in the request headers. Let’s take a look at how to set request headers in JMeter.

To set headers with the request, we need to use the HTTP Header Manager.

You can add it by right-clicking on the HTTP Request SamplerAddConfig ElementHTTP Header Manager.

Adding Headers with the HTTP Request

For now, let’s set a dummy header with the name x-token and the value codekru.

Now, click on the Run button and check the results.

Headers shown in the results

There are a few other components of the HTTP Request Sampler that we haven’t explored yet. Let’s go through them one by one.

When you’re testing APIs or websites using JMeter, sometimes the server doesn’t give you the final response directly. Instead, it sends a redirect response — like a 301 or 302 — telling your client (JMeter, in this case) to go somewhere else to get the actual content.

This is where the “Follow Redirects” and “Redirect Automatically” options come into play.

Let’s understand both. We’ll use the http://httpbin.org/redirect/3 API, which performs three redirects before finally landing on the http://httpbin.org/get endpoint.

  • When this option is checked, JMeter will follow the redirects.
  • It records both the original request and the redirected request.
  • So, you’ll see multiple results in the View Results Tree or logs

Let’s execute the http://httpbin.org/redirect/3 API and check the “View Results Tree” Listener for the responses.

Steps for Configuration:

  1. Name: Follow Redirect
  2. Server Name or IP: httpbin.org
  3. Protocol: http
  4. HTTP Method: GET
  5. Path: /redirect/3
  6. Select the “Follow Redirects” checkbox
Checked "Follow Redirects" option in HTTP Request Sampler

Now, run the Test Plan. You’ll notice multiple requests appearing in the ‘View Results Tree’, as shown in the image below.

Multiple calls in case of follow redirects
  • When this is checked, JMeter behaves like a browser and automatically follows redirects under the hood.
  • Only the final response is recorded.

So, let’s use the same http://httpbin.org/redirect/3 API once again and select the “Redirect Automatically” option this time.

Redirect Automatically option in HTTP Request Sampler

Run the Test Plan.

"Redirect Automatically" Result

As you can see, only one entry was recorded, and it displays only the final response.

In Simple Words, the Key Difference:

  • Follow Redirects shows you both the redirect and the final destination, step by step.
  • Redirect Automatically hides the redirect step and shows you only the final destination.

Let’s again use the http://httpbin.org/redirect/3 API and create two HTTP Request Samplers: one with Follow Redirects enabled and the other with only Redirect Automatically enabled.

Please see the video below for the Test Plan configuration and execution

Here, we can clearly see that the ‘Follow Redirects’ option displays all the intermediate redirects, whereas ‘Redirect Automatically’ only shows the final response.

The Port field in the HTTP Request Sampler specifies the port number to which the request should be sent on the server.

Port number in HTTP Request Sampler

By default:

  • HTTP requests use port 80
  • HTTPS requests use port 443

If you’re sending requests to these default ports, you can leave the Port field blank, and JMeter will automatically use the appropriate port based on the protocol (HTTP or HTTPS).
However, if the server uses a non-standard port, you’ll need to enter it in this field explicitly.

This is it. We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Related Articles –

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *