APIs Unleashed 03:
Handling REST APIs

API unleashed banner

REST (Representational State Transfer) is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other. REST-compliant systems, often called RESTful systems, are characterized by how they are stateless and separate the concerns of client and server. Most commonly used nowadays.

Separation of Client and Server

In the REST architectural style, the implementation of the client and the implementation of the server can be done independently without each knowing about the other. This means that the code on the client side can be changed at any time without affecting the operation of the server, and the code on the server side can be changed without affecting the operation of the client.


As long as each side knows what format of messages to send to the other, they can be kept modular and separate. Separating the user interface concerns from the data storage concerns, we improve the flexibility of the interface across platforms and improve scalability by simplifying the server components. Additionally, the separation allows each component the ability to evolve independently.


By using a REST interface, different clients hit the same REST endpoints, perform the same actions, and receive the same responses.


The REST architectural style describes six constraints originally described by Roy Fielding in his Ph.D. thesis. They are:


  • – Uniform interface
  • – Stateless
  • – Cacheable
  • – Client-server architecture
  • – A layered system
  • – Code on demand (optional)


Twitter, Google, Slack and Blogger are famous REST API examples.

Postman: Handling REST APIs

Let's see how we can handle REST APIs using POSTMAN

Now, we will see an example of REST API endpoint and how handle that in Postman, and how to parse the response of it.

Mostly REST will follow the JSON format request and response. We already discussed how to create requests in our Intro blog here, please refer this if needed.

Step 1: Create a new request and paste the endpoint URL as https://vpic.nhtsa.dot.gov/api/vehicles/getallmanufacturers?format=json


Step 2: By clicking “Send” you receive a lengthy JSON response


How do we parse the response?

First things first, once we receive the response we should first store the entire response in an JSON object and then we use the variable for future purpose.

Now the response is stored as a JSON object and we access using dot or bracket notations.

accessing responses
accessing responses

Example 1: Get the value of Count key

For example, to get the count key in the response:

count key

Example 2: Get first element from "Results" array

Sometimes, you can see an array inside the response. So just be cautious in handling them.


Here “Results” key is an array. How do we access the first element in that?


Response looks like below:

array response

Example 3: Get all manufacturer names element from "Results" array

Okay perfect! We are able to get the first element in the array “Results”, in case if we need to get all the elements we just need to iterate the response. There are few methods to do that. For a beginner, traditional “for” loop will be pretty simple to learn.


Let’s see how to handle the looping through the entire response:

looping response

Now we are trying to save that as an array inside an environment variable. The Manufacturer names are stored like below:


We can use this array for future requests or for any assertions to this API request.


For any JSON response, no matter how big is that you can definitely parse and break down into the tiny elements to handle your future requests.


Two Tips here,


When you are finding difficulty parsing the bigger JSON response:

  1. 1. Always “Console“. console.log will definitely help you to understand the response hierarchy.
  3. 2.  https://jsonpathfinder.com/ this simple tool will help you to understand your response better. To know more about this, please read this blog.


So for any kind of bigger response, now you can parse easily and store them as variables for future use.


Anything which goes wrong in parsing will affect your Assertions which you might create later for your APIs.

KarateDSL: Handling REST APIs

Let's see how we can handle REST APIs using KarateDSL

KarateDSL is one of the most effective API Automation tools available in the market.

Let us understand, Why it is said so ?

Example 1: Get the value of Count key

Step 1: Create a feature file under src/test/java.


Step 2: To get the value of the “Count” key from the API JSON response, use the code below.


To check whether the JSON path is accurate, you can use Jayway JsonPath Evaluator.


Please note that “def” is used to define and store a variable that will return a JSON-formatted response automatically.


Here, “response.Count” is the path to get “Count” value

Step 3: Replace the name of the feature file in the Runner file you wish to run, then hit the execute icon (I am using Eclipse IDE).


Step 4: Copy the html link from console and paste into any browser to see the full report, as shown below.


“count” variable is now having value of “Count” Key.


This variable can be used for assertions and matching in the future.

Example 2: Get first element from "Results" array

We may use same feature file to get the first element from “VehicleTypes” array.


We pass the index number in square brackets “[index]” to get data from a desired index of an array, as demonstrated below:


Since we are reusing the same feature file, you can simply reload the previous karate report after running the script using the UserRunner file.


Note: If you’re creating a new feature file, copy the latest report URL to ensure you get the right one.

Example 3: Get all manufacturer names element from "Results" array

The same feature file may be used to extract the entire list of manufacturer names from the JSON response.


To get the JSONPath of the requested element, there are two methods:


Method 1: By defining Absolute path –

absolute path

Here, “$” is referred as the “root member object” in JsonPath and “*” (Wildcard) indicates all or variable/numeric available anywhere in the JSON.


To know more about JSONPath evaluation in karate, refer this link.


Method 2: Using Dynamic or relative path –

Dynamic or relative path

Double dots “..” is used to deep scan the JSON response to find the requested element.


The feature file is as follows:

feature file

You may either reload the previous karate report (because we’re using the same feature file) or copy-paste the html report URL into your browser after executing the script with the UserRunner file.

report 1
report 2

After execution, both methods return the same set of data.


I would recommend you to use dynamic/relative method to get the desired values from JSON response.


We don’t need any loops to iterate through the “Results” array and get the manufacturer name, this is the charm of the KarateDSL. Hence, It is said that KarateDSL have- Simpler and more powerful alternative to JSON-schema for validating payload structure and format.


With the help of the examples above, we’ve seen JSON parsing methods. I hope you found this helpful in understanding how we may handle REST API with Karate DSL.

Viola, we dealt with an REST API example and how to parse the response and store them as variables. So, try to play around with different examples and get familiarized with the response parsing, because you need to be strong in this which helps you to handle the “Assertions” which we will be covering in our future posts.


In the next post, we can see how to deal with SOAP APIs and parse the XML response.


Dear readers, thank you for your time. See you in the next few days with our next topic!!

Missed Previous Chapters?

Know Our Synapses

Meet the brains behind this learning series!

Let's Together

Follow Us

Stay updated with all the latest contents & announcements

Share this:

Like this:

Like Loading...

Up ↑

%d bloggers like this: