Up until now, we already covered sending a GET Request using Karate. If you haven’t checked that, go for it by clicking this link. Now, in this example, we are going to see “How to send a HTTP POST JSON request using Karate Dsl to a Rest API endpoint”.

Post_Request_Karate_Dsl_Featured_Image_Techndeck

Simple example of POST JSON Request using Karate...!!! Share on X

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

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

Let’s begin:

1. What is HTTP POST Request?

POST is one of the most common methods of HTTP which is used to send data to a server to create/update the resource. Data sent to the server is in the form of either Request Body / Request Parameters which is basically used to create or update the resource on the server.

Some key points of POST requests:

  • POST requests parameters doesn’t store in the browser history
  • POST requests are used to send data to the server to create or update the resource
  • POST requests cannot be cached
  • POST requests are secure as compared to GET because parameters/data doesn’t store in browser history
  • POST requests parameter data is unlimited as there are no length restrictions
  • POST requests cannot be bookmarked
  • POST requests allow ASCII characters
  • POST requests are difficult to hack

2. How to send POST request using Karate?

Before proceeding, lets set up your project by following this article. 

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

At the above resource URL, we are going to submit data in the form of JSON to create an employee.

JSON Request Body:

Code to send the POST request to the above mentioned Service Endpoint (postRequestTest.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 create 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”: “Isha”,”salary”: “5000”,”age”: “20”,”id”: “2205”} : This line of code helps to check if the response coming from the service exactly matches with the specified expected content. Here it failed because of ‘duplicate entry’ issue.

Testng Report:

Post_Request_Junit_Report_Karate_Techndeck

Eclipse Console Output:

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

Simple example of POST Request using Karate...!!! 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