So far, we have covered sending a GET & POST Request in our tutorial on Karate testing framework. If you haven’t checked that, lets check ‘Sending GET Request’ by clicking this link and also check ‘Sending POST request’ using this link. Now, in this example, we are going to see “How to send a HTTP PUT request using Karate Dsl to a Rest API endpoint”.

Put_Request_Karate_Featured_Image_Techndeck

Simple example of PUT Request using Karate...!!! Share on X

In this tutorial, we are going to cover below topics:

  1. What is HTTP PUT Request?
  2. How to send PUT request using Karate?
  3. Response Validation

Let’s begin:

1. What is HTTP PUT Request?

PUT method requests for the enclosed entity are stored under the Request-URI. An update operation will happen if the Request-URI refers to already existing resource otherwise there will be a create operation takes place if Request-URI is a valid resource URI.

Some key points of PUT requests:

  • PUT is idempotent means if you try to make a request multiple times, it would result in the same output as it would have no effect.
  • PUT should be used when you want to modify a resource which is already a part of resource collection as PUT method would replace the resource entirely.
  • PUT resource can be cached.

 

2. How to send PUT request using Karate?

Let’s take an example of one of the API PUT endpoint available at the above-mentioned website which is ‘/update/{id}’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/update/{id}’.

At the above resource URL, we are going to submit data in the form of JSON to update an existing employee which is having ‘id’ as ‘4710’.

NOTE: This ‘id’ belongs to the employee which is generated during the POST call to create the employee. So, this id : 4710 generated for me when I had executed the create employee using the POST call. In order to get yours, first execute the POST Request call available at this link and grab the ‘id‘ from the response and use it in your PUT Request Test below in place where I am using ‘4710’. 

JSON Request Body:

Code to send the PUT request to the above mentioned Service Endpoint (putRequestTest.feature):

 

Let’s try to understand the code:

1. Specifying the Feature & Scenario (typical cucumber format)

2. Setting up Endpoint URI in ‘Given’ 

Endpoint URI is the address to the Resource. And, by this particular line of code, we are specifying to Karate to use “dummy.restapiexample.com/api/v1” as the root URL of the service.

3. Specifying the ‘json’ formatted employee data that need to be sent to the service to update the employee

4. Specifying the type of request (POST)

5. Checking up Status Code & other Response validations in ‘Then’

 

3. Response Validation

a. status 200 : It will check the status code coming back from the service is 200

b. print ‘Response is: ‘, response : This line of code will print the response from the service in the console.

c. match response == {“name”: “put_test_employee”,”salary”: “1123”,”age”: “23”} : This line of code helps to check if the response coming from the service exactly matches with the specified expected content.

 

Testng Report:

Put_Request_Testng_Report_Karate_Techndeck

Eclipse Console Output:

 

That’s it, it’s that simple to make a PUT Request with JSON body using Karate Dsl API: ?

Simple example of Karate PUT Request example ...!!! Share on X

 

If you like this post , please check out my other useful blog posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak