karate DSL : POST and GET API calls

Reading Time: 3 minutes

In the previous blog, we saw how we can set up a project in karate DSL and saw the folder structure in this blog we will see how to make POST and GET API calls and how it can be automated using karate DSL.

  • First, we need to create a .feature file where we will write our test cases. We have already seen the file and folder structure in our previous blog Karate DSL : Getting started

.feature file

  • In this file, we will be defining the URL which will be used in our requests.
  • Also defining the request body which will be used in our POST requests.
  • We will also create a POST request and put assertions on its response.
  • And finally, create a GET request and validate that the data which we have sent in our POST request is stored correctly in the database or not.
Feature: POST and GET API calls.

  Background:
    * url 'https://43db3005-4ed9-4bab-b0a3-bb066e79e816.mock.pstmn.io'
    * def user =
      """
      {
   "firstName":"James",
   "lastName":"White",
   "username":"jamesWhite",
   "email":"james@gmail.com"
      }
      """

  Scenario: create a user from given details.

    Given path '/POSTUser'
    And request user
    When method post
    Then status 201
    And match $.Status == '#present'
    And match $.Status == 'OK'

  Scenario: Validate the user is created with valid details.

    Given path '/GETUser'
    When method get
    Then status 200
    And match $.firstName == user.firstName
    And match $.lastName == user.lastName
    And match $.username == user.username
    And match $.email == user.email


  
  • url : Defines the URL which we want to hit and get some data. Asterisk (*) means that this URL will be used in all the requests in the particular feature file.
  • def: Defines the values of the variables.
  • path : It defines the path of the URL. So, if you are using same base URL but are required to hit multiple endpoints to can use this feature.
  • method : It defines the type of HTTP method which is to be used in the request.
  • status : It is the expected status code from the service. It validates that the service returns the correct status code in response.
  • print : It is used to print the response body of the request.
  • match : Is is used for asserting values on the response body.
  • $ : Denotes response body.

Runner file

  • Use this runner file to run your .feature file.
package examples.users;

import com.intuit.karate.junit5.Karate;

class TestUsersRunner {
    
    @Karate.Test
    Karate testUsers() {
        return Karate.run("POSTandGETCall").relativeTo(getClass());
    }    

}
  • This file requires us to provide the name of our .feature file and then we can execute the file.

So this was a short blog on how to make POST and GET API calls using karate DSL. In the next blog, we will see how to read the request body from another file.

References : https://github.com/intuit/karate


Knoldus-blog-footer-image

Written by 

Ankur is a QA Consultant having experience of more than 1.5 years. He is familiar with the core concepts of manual and automation, postman and Newman are his expertise. He is always eager to learn new and advance concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies include watching web series and getting to know about all the latest gadgets that come in the market.