RestAPI Testing in JMeter

Restful Web Service is a lightweight, maintainable, and scalable service that is built on the REST architecture.

  • Restful Web Service, expose API from your application in a secure, uniform, stateless manner to the calling client.
  • The calling client can perform predefined operations using the Restful service.
  • The underlying protocol for REST is HTTP.
  • REST stands for Representational State Transfer.

The Key Elements of a RESTful implementation are as follows:

Resources :

Request Verbs :

  • The Request Verbs describe what you want to do with the resource.
  • A browser issues a GET verb to instruct the endpoint it wants to get data.
  • However, there are many other verbs available, including things like POST, PUT, and DELETE.
  • So in the case of the example https://chercher.tech/sample/api/product/read?id=2180 The web browser is issuing a GET Verb because it wants to get the details of the employee record.

Request Headers :

  • These are additional instructions sent with the Request. These might define the type of response required or the authorization details.

Request Body :

  • Data is sent with the Request.
  • Data is normally sent in the Request when a POST request is made to the REST API.
  • In a POST call, the client tells the web service that it wants to add a resource to the server.
  • So, the request body would have the details of the resource which is required to be added to the server.

Response Body :

  • This is the main body of the response.
  • So in our example, if we were to query the web server via the Request https://chercher.tech/sample/api/product/read?id=2180 the server returns a JSON body with all the details of the Product in the Response Body.

Response Status codes :

  • These codes are the general codes that are returned along with the response from the webserver.
  • An example is the code 200 which is typically returned if there is no error when returning a response to the client.

When the client makes any request to this web service, it can specify any of the normal HTTP verbs of GET, POST, DELETE and PUT. Below is what would happen If the respective verbs were sent by the client.

  • POST : This would be used to create a new employee using the RESTful web service.
  • GET : This would be used to get a list of all employees using the RESTful web service.
  • PUT : This would be used to update all employees using the RESTful web service.
  • DELETE : This would be used to delete all employees using the RESTful web service.

Let's take a look from a perspective of just a single record. Let's say there was an employee record with the employee number of 1.

The following actions would have their respective meanings.

  • POST: This would not be applicable since we are fetching data of employee 1 which is already created.
  • GET : This would be used to get the details of the employee with an employee no as 1 using the RESTful web service.
  • PUT : This would be used to update the details of the employee with an employee no as 1 using the RESTful web service.
  • DELETE: This is used to delete the details of the employee with an employee no as 1.
Why Restful:

Restful mostly came into popularity due to the following reasons :

Heterogeneous languages and environments :

  • This is one of the fundamental reasons which is the same as we have seen for SOAP as well.
  • It enables web applications that are built on various programming languages to communicate with each other.
  • With the help of Restful services, these web applications can reside in different environments, some could be on Windows, and others could be on Linux.

The event of Devices :

  • Nowadays, everything needs to work on Mobile devices, whether it be the mobile device, the notebooks, or even car systems.
  • Can you imagine the amount of effort to try and code applications on these devices to talk with normal web applications? Again Restful APIs can make this job simpler because as mentioned in point no 1, you really don't need to know what is the underlying layer for the device.

Finally in the event of the Cloud :

  • Everything is moving to the cloud.
  • Applications are slowly moving to cloud-based systems such as in Azure or Amazon.
  • Azure and Amazon provide a lot of API's based on Restful architecture.
  • Hence, applications now need to be developed in such a way that they are made compatible with the Cloud.
  • So since all Cloud-based architectures work on the REST principle, it makes more sense for web services to be programmed on the REST-based architecture to make the best use of Cloud-based services.

Recording Test Scripts in JMeter

API/Web Services Testing in JMeter

I have some sample APIs, let us see how to execute these APIs.

The following example demonstrates the execution of Employee details by using the Get Request API.

  • Launch JMeter in your system and then add Thread Group to the Test Plan. Here I am concentrating mainly on API testing so I am keeping the Number of Users as 1 only.
    thread-group-api-testing-jmeter
  • Next, add an HTTP Sampler Request, which is nothing but our first GET Request to the Thread group.
  • The Request is going to get all the data that is present in the server.
  • Enter the Server or IP address as dummy.restapiexample.com
  • And enter the path as /api/v1/employees
  • Select the HTTP Request Method as GET as below.
    http-request-details-get-request-jmeter
  • Next, add View Result tree Listner to the Thread Group.
  • Now save and run the thread group, after the execution the get request API will get all the employee details.
    execution-of-get-request-jmeter
  • The Request is as below.
    get-request-jmeter
  • And the response will be the employee details as below.
    {"status":"success","data":[{"id":"1","employee_name":"Tiger Nixon","employee_salary":"320800","employee_age":"61","profile_image":""},{"id":"2","employee_name":"Garrett Winters","employee_salary":"170750","employee_age":"63","profile_image":""},{"id":"3","employee_name":"Ashton Cox","employee_salary":"86000","employee_age":"66","profile_image":""},{"id":"4","employee_name":"Cedric Kelly","employee_salary":"433060","employee_age":"22","profile_image":""},{"id":"5","employee_name":"Airi Satou","employee_salary":"162700","employee_age":"33","profile_image":""},{"id":"6","employee_name":"Brielle Williamson","employee_salary":"372000","employee_age":"61","profile_image":""},{"id":"7","employee_name":"Herrod Chandler","employee_salary":"137500","employee_age":"59","profile_image":""},{"id":"8","employee_name":"Rhona Davidson","employee_salary":"327900","employee_age":"55","profile_image":""},{"id":"9","employee_name":"Colleen Hurst","employee_salary":"205500","employee_age":"39","profile_image":""},{"id":"10","employee_name":"Sonya Frost","employee_salary":"103600","employee_age":"23","profile_image":""},{"id":"11","employee_name":"Jena Gaines","employee_salary":"90560","employee_age":"30","profile_image":""},{"id":"12","employee_name":"Quinn Flynn","employee_salary":"342000","employee_age":"22","profile_image":""},{"id":"13","employee_name":"Charde Marshall","employee_salary":"470600","employee_age":"36","profile_image":""},{"id":"14","employee_name":"Haley Kennedy","employee_salary":"313500","employee_age":"43","profile_image":""},{"id":"15","employee_name":"Tatyana Fitzpatrick","employee_salary":"385750","employee_age":"19","profile_image":""},{"id":"16","employee_name":"Michael Silva","employee_salary":"198500","employee_age":"66","profile_image":""},{"id":"17","employee_name":"Paul Byrd","employee_salary":"725000","employee_age":"64","profile_image":""},{"id":"18","employee_name":"Gloria Little","employee_salary":"237500","employee_age":"59","profile_image":""},{"id":"19","employee_name":"Bradley Greer","employee_salary":"132000","employee_age":"41","profile_image":""},{"id":"20","employee_name":"Dai Rios","employee_salary":"217500","employee_age":"35","profile_image":""},{"id":"21","employee_name":"Jenette Caldwell","employee_salary":"345000","employee_age":"30","profile_image":""},{"id":"22","employee_name":"Yuri Berry","employee_salary":"675000","employee_age":"40","profile_image":""},{"id":"23","employee_name":"Caesar Vance","employee_salary":"106450","employee_age":"21","profile_image":""},{"id":"24","employee_name":"Doris Wilder","employee_salary":"85600","employee_age":"23","profile_image":""}]}‚Äč

    response-data-of-employee-jmeter

  • To Verify whether the response code 200 or not, we need to add Response Assertion to the thread group.
  • And then add below details in the Response Assertion.
    response-assertion-in-api-testing-jmeter
  • Save and run the thread group, after the execution, the Request will be passed without any errors, that means the response code 200 is correct.

The following example demonstrates the execution of Specific Product details

  • Add One More HTTP Request to the current test plan and then mention the below details.
    getting-specific-product-details-jmeter
  • We are going to get the details of the product where the product id is 2180.
  • Save and run the request, you will see the details of the specific product.
    specific-product-details-jmeter
  • And also we can verify the product name of the corresponding product id.
  • Add Response Assertion to the Get Specific Details and verify the Text Response. So the particular response id should contain the Product name as dan.
    response-assertion-with-name-dan-page-jmeter
  • Now Save and run the Thread Group.
  • The Response body contains the same name of the product, so the request has been passed without any error.
    same-name-with-response-assertion-jmeter

How to Use JMeter for Performance Testing

Post Request with JMeter

The Following Example demonstrates Creating a new employee record by using the Post Request.

  • Add HTTP Sampler Request to the Thread Group.
  • Next, enter the Server address and Path details and click on the Body Data and enter the new employee details as shown below.
    http-post-request-jmeter
  • The new employee details are as below.
    {"name":"ABCDEF","salary":"123000","age":"27"}
  • Here employee id will be created by the server.
  • In the post request also we need to verify the employee details, here I am going to verify the employee name in the response assertion.
  • Add Response Assertion to the Post Request and enter the Text Response as ABCDEF.
    response-assertion-verify-employee-name-jmeter
  • Save and run the thread group, after execution, if you open the response data you find that the new employee details have been created with new employee id.
    response-data-with-new-employee-details-jmeter

HTTP Cookie Manager in JMeter

PUT Method Using JMeter

The following example demonstrates updating the existing employee details by using the PUT API

  • Add a new HTTP request to the Thread Group and then enter the server and path details as below.
  • Here I am going to update the employee details of employee id 55.
  • Click on the Body Data and update the employee Name to ABCDGH as below.
    updating-the-employee-details-jmeter
  • Next, add Response Assertion to the PUT Request and enter the Test Response as below.
    response-assertion-for-put-request-jmeter
  • Save and run the thread group, after the execution the new employee details will be updated.

Regular Expression Extractor in JMeter

Delete Method With JMeter

The following example Demonstrate Deleting the Employee detail by using the DELETE API

  • Add HTTP request to the Thread group and then add Server IP and Path Details.
  • And also enter the Product ID of which you want to delete the details.
    deleting-product-details-jmeter
  • Save and run the Thread Group, after execution the particular product details have been deleted.
    result-tree-of-delete-product-jmeter
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions