In the previous blog, we saw how to make GET and POST calls using karate DSL. In this blog, we will see how we can use karate DSL by reading data from external files and also how we can change the values of the request body.

  • Firstly we will need to create a simple JSON file from which we will be reading the body for our request.
{
  "firstName":"James",
  "lastName":"White",
  "username":"jamesWhite",
  "email":"james@user.com"
}
  • Now we will create a .feature file. Which will make a POST call reading the request body from an external file using this line of code
And def BodyOfRequest = read('/home/ankur/Desktop/KARATE/new/karateBlog/src/test/java/examples/users/dataFiles/userData.json')
And request BodyOfRequest
  • In this line, we are defining a variable BodyOfRequest, reading a an external file userData.json which is stored locally in our system using the read function and store the content of the file it the variable.
  • Request function is used to give an input body to our request.
Feature: POST API call.

  Background:
    * url 'https://43db3005-4ed9-4bab-b0a3-bb066e79e816.mock.pstmn.io'

  Scenario: create a user from given details.

    Given path '/POSTUser'
    And def BodyOfRequest = read('/home/ankur/Desktop/KARATE/new/karateBlog/src/test/java/examples/users/dataFiles/userData.json')
    And request BodyOfRequest
    When method post
    Then status 201
    And match $.Status == '#present'
    And match $.Status == 'OK'

#scala #dsl #karate dsl #data

karate DSL : Reading data from external files
22.90 GEEK