Wednesday 3 December 2014

Jmeter: Rest Services- Passing one HTTP response to another HTTP request.



Jmeter: Rest Services- Passing one response as another request.

Suppose I have a HTTP request and based on the response, the next request will be delivered.
For Example: My 1st HTTP request gives a list of countries and based on country we get as response the 2nd HTTP request gives the list of cities.

 

Steps for Passing One HTTP response to another HTTP request.

1.       Add Thread Group to your Test plan.     

2.  Now configure the HTTP request details in the HTTP request Defaults

 


The server/IP name:  It can be Server IP of your request or localhost
Port Number: It is the port server is accessing.
Path: It is the path name specified in the URL
Parameters: If the request has some parameters, then they can be specified, else left blank.

3.       Parametrize the username and password by adding CSV data config and keep the username and passwords in a text file. 

4.       In this example, the HTTP request returns the “customer id”. We have replace the value of customer id by a regular expression.   

 

5.       Now right click on the HTTP request and add a “Regular Expression Extractor”.  




6.       Find out the regular expression which will replace your parameter value in the response. 

7 .       Replace the regular expression in place of the parameter as shown below.  




Here, reference name: It is the name of the variable, which you want to use further.
Regular expression: I replaced the value of dynamic value by
{"Status":"True","Response":{"status":"success","customerId":"(.+?)"}}
Template: $1$
Match No: 0
Default Value: Error

8.       By adding the regular expression extractor, you can collect the value of the customer id in the variable “custid” and use that in later request. 

9.       Now add another HTTP request for which the above response will be used. 

10 .       Here in my example the response was passed in the URL of the 2nd request. My 2nd request looked like as below

  http://localhost:8080/testArchii/getlist/52/NA/NA/1/10 11.      

 Here “52” is the customer id. Now I will replace the value of customer id by parameter, I got from previous output. It will be ${custid}. 

12.       The 2nd request will look like as below.

 

13.       Now add a listener to view the results. Right click on the thread group --> add listener --> add view result tree.  

 

14 .       Now save the test plan and run the script. 

15.       View the results in View Result Tree.  

 

In this example, request from one Restful service with JSON response, was passed to another Restful service to get the response. 

No comments:

Post a Comment