How to handle Correlation in JMeter?
Introduction
Correlation is one of the important aspects of JMeter. It is the process of fetching dynamic data from a response and using it in another request without manual intervention. This is similar to the Request Chaining concept in Postman. Regular Expression Extractor plays an important role in Correlation. It extracts values from the response and stores them in a variable that can be used in another request.

Need of Correlation
First, we have to identify the static and dynamic data where static data does not need any correlation. In this blog, we are going to use the LMS application whereas ProgramId is dynamic. For every POST request, a unique ProgramId is created by the server and it can be used for further requests. This correlation is done with the help of the Regular Expression Extractor which reduces human effort and time and increases efficiency.
Scenario
1. Create a Program in LMS – https://lms-backend-service.herokuapp.com/lms
2. Fetch the newly created ProgramId and store it in variable
3. Pass the ProgramId to Get request as input and get the Program by ProgramId
1. Create a new Program in LMS
Step 1: To create a Thread Group
· Open JMeter and right-click on Test Plan -> Add -> Threads (Users) -> Thread Group

Step 2: To add a Sampler
· Right-click on Thread Group-> Add -> Sampler -> HTTP Request

Step 3: To add User Defined Variables.
We can declare the URL and Port number as User Defined Variables. This can be declared as a global variable by placing it under Test Plan. Also, it can be declared as a local variable by placing it under the Thread Group level or Sampler / Request level.
Here we are declaring the baseurl as a Global variable.
· Right-click on Test Plan-> Add -> Config Element -> User Defined Variables.

· Click on Add button.

· Enter Value for baseurl.

Step 4: To add HTTP Request Defaults.
We can set the default values for the HTTP Requests in the Test Plan using HTTP Request Defaults. In case we don’t specify any baseurl in our request, it will take the baseurl from HTTP Request Defaults. If a request has any baseurl values, it will consider the local variable.
· Click on Test Plan –> Add -> Config Element -> HTTP Request Defaults

Enter Protocol as : https (If it is an https protocol)
Enter Server Name or IP as : ${baseurl}

Note: We can drag and drop the components like HTTP Request Defaults, User Defined Variables, HTTP Header Manager under Test Plan for better organizing.

Step 5: To add the Request name and details.
Change the Name as : CreateAndSaveProgram
HTTP Request : POST
Path :/lms/saveprogram
Body data :
{
"programName": "Jan23-APICoders-SDET-100-011",
"programDescription": "Database Administration",
"programStatus": "Active",
"creationTime": "2023-03-06T04:01:42.616+00:00",
"lastModTime": "2023-03-06T04:01:42.616+00:00"
}

Step 6: Add Header Manager to include Header details for the request.

· Click on Add button in HTTP Header Manager.

· Enter Accept and Content-Type fields in HTTP Header Manager as application/json. If we miss this step, we will get Response Code 415 – Unsupported Media Type error.

Step 7: Add Assertion to validate the Response Code.
· Right-click on request -> Add -> Assertions -> Response Assertions
· Select Response Code.
· Click Add button.
· Enter 201.

· Click the Green color Run icon.

· Click on View Result Tree. We can see CreateAndSaveProgram request is successfully run with a green tick mark.
· Click on CreateAndSaveProgram .
· Select SamplerResult tab. We can find the Response Code as 201 for POST requests.

· Navigate to Response Data tab.
· Copy the entire response.
· Open a chrome browser.
· Enter the url as https://jsoneditoronline.org/
· Paste the Response Data content on the left-hand side panel.
· Click Copy > button

· When we create a new Program, the server generates a new ProgramId every time dynamically. We are going to pass it to the GET request as input. So copy the field “programId” from the right-side panel.
2. Fetch the newly created ProgramId and store it in variable
Step 1: After processing the POST request, the server sends the Response in JSON format. To extract a particular value from the JSON Response, we need to use the JSON Extractor component.
· Right-click on CreateAndSaveProgram -> Add -> Post Processors -> JSON Extractor

· Enter JSON Path expressions as programId which we copied from the right-side panel.
· Change it to $.programId
· Enter Names of created variables as programId

3. Pass the ProgramId to Get request as input and get the Program by ProgramId
Step 1: Create another Request for Get Program by ProgramId.
Right-click on Thread Group -> Add -> Sampler -> HTTP Request
Change Name as : GetProgramByProgramId
Select HTTP Request as : GET
Enter Path as : /lms/programs/{programId}

Note: The program Name should be unique, otherwise it will throw an error message in the POST request. So, I changed the Program Name as
{
"programName": "Jan23-APICoders-SDET-100-015",
"programDescription": "Database Administration",
"programStatus": "Active",
"creationTime": "2023-03-06T04:01:42.616+00:00",
"lastModTime": "2023-03-06T04:01:42.616+00:00"
}
Step 2: Click the View Results Tree and Green Run icon.

Step 3: Click CreateAndSaveProgram, Navigate to ResponseData tab. A new Program is created with ProgramId 15752.

Step 4: Click GetProgramByProgramId, Navigate to ResponseData tab.

Note: To clear the View Results Tree, click on Clear All icon.

Conclusion
In this blog, we covered a very important concept Correlation in JMeter. Dynamic value from a Response is stored and passed to another request as input during execution. Regular Expression Extractor extracts the value from the response and stores it, which can be used in further requests that need dynamic data.