This illuminating article will delve deep into the intricacies of seamlessly extracting and channeling JSON values between API requests, i.e., request-response Chaining. From creating and parameterizing REST API requests to scripting a symphony of response interactions, we will illuminate every facet of the process. Brace yourself to witness the synergy of theory and practice as we unveil the outcome through meticulous execution.
We will create REST API requests and test cases based on it. The response generated after running the Rest API request will be in JSON format. We will extract the desired value from the response, store it in a variable, and then call it in another request.
To extract the value from a JSON response, we will use the class” JsonSlurper.” The class passes JSON text into Groovy objects. The course contains the “parseText” method, which converts the JSON string into a list or map of objects.
Creating Rest API Request to use for Chaining
We will use the sample website “https://reqres.in/“; it contains different requests for different Rest API methods. We will create a GET method request to get a list of users as a JSON response, then extract the value of “first name” and create a PUT request to update the list with the “first name” value extracted from the first response.
1. Creating REST API request(GET method) in Katalon Studio
We will start with creating a new Project in Katalon Studio and select the type “API/Web Service” while creating the project. This will make the Katalon Studio UI more convenient for API Testing.
Sample website with REST API we’ll be using: “https://reqres.in/.” It has different requests for different methods of REST API. We’ll use LIST USERS, CREATE, UPDATE, and DELETE requests.
Following are the steps for creating the requests:
- The first request we’ll be creating is to list users; go to the sample website URL > click on “LIST USERS”> copy the request URL: “https://reqres.in/api/users?page=2”
- Create a new folder as” REST” in the Object repository, as API requests are stored inside the Object Repository section.
- Right-click on the folder > New > Web Service Request.
- Name it “GetUser,” select “RESTful” as the request type, and enter the request URL copied.
- Add details for the request: select Method as GET, nobody as GET request, and query parameters automatically detected from the request URL. We can add verifications using the snippets available in the Verification section.
- Execute the request by clicking on “Test Request” or “Test Request and Verify” (if you have added verification in the request)
- The response will be generated in JSON format containing the status code, body, header, verification, and validation logs.
2. Creating REST API request (PUT method) in Katalon Studio
- We will create a request to update a user. Go to the sample website URL > click on “UPDATE” > copy request URL: “https://reqres.in/api/users/2”
- Create a new Web Service Request. Name it” UpdateUser”.
- Add details for the request: Select Method as PUT. PUT request also requires the body in JSON format containing data to be updated (update user). We can copy the JSON format of the method from the website and paste it into the HTTP Body.
- Execute the request and analyze the response.
Below is the image of the object repository after creating both requests:
Parameterizing the input in the PUT request
Open the “UpdateUser” request which we created
- Variables > Create a variable “username” and enter a default value, “John.”
- HTTP Body > Replace the hardcoded value of the name” with “${username}.”
- We can run and check the response to see that the default value has been updated in the name
Note: We can also create a Global variable instead and call it in the request as shown below:
Creating a Test Case
We will create a test case and name it “APIChainingJsonDemo” and add both “GetUser” and UpdateUser” requests.
We must change to script mode and use the JsonSlurper class and its method parseText to extract the value from the Json response. Below is the final script after using the class and method:
Executing the Test
Run the test case using the “Run” button and check the Logs and console. We have successfully chained the Rest API request response in Katalon Studio.
Note: We can also add a verification snippet in the “UpdateUser” request to verify that the value is correctly updated and is the same as the value extracted from the “GetUser” request.
Conclusion
- Comprehensive Exploration: We’ve delved into the intricate world of Request-Response Chaining in Katalon Studio for REST APIs using JSON.
- Foundational Understanding: We’ve grasped the core tools that underpin this strategy, including the “JsonSlurper” class.
- Dynamic Flow: We’ve demonstrated how to fluidly move data between requests, unleashing the power of API chaining.
- Seamless Value Extraction: By extracting, manipulating, and passing JSON values, we’ve showcased the technique’s seamless potential.
- Hands-on Experience: Through practical scripting and execution, we’ve embraced the technique’s practicality.
- Enhanced Skill Set: Armed with this skill, you can elevate your API testing capabilities and construct intricate, interconnected tests in Katalon Studio.