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”.
Simple example of POST JSON Request using Karate...!!! Share on X
In this tutorial, we are going to cover below topics:
- What is HTTP POST Request?
- How to send POST request using Karate?
- 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:
1 2 3 4 5 | { "name":"Isha", "salary":"5000", "age":"20" } |
Code to send the POST request to the above mentioned Service Endpoint (postRequestTest.feature):
1 2 3 4 5 6 7 8 9 10 | Feature: Create Employee Scenario: Verify that a new employee is successfully getting created Given url 'http://dummy.restapiexample.com/api/v1/create' When request { "name":"Isha", "salary":"5000", "age":"20" } And method post Then status 200 And print 'Response is: ', response And match response == {"name": "Isha","salary": "5000","age": "20","id": "2205"} |
Let’s try to understand the code:
1. Specifying the Feature & Scenario (typical cucumber format)
1 2 3 | Feature: Create Employee Scenario: Verify that a new employee is successfully getting created |
2. Setting up Endpoint URI in ‘Given’
1 | Given url 'http://dummy.restapiexample.com/api/v1/create' |
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
1 | When request { "name":"Isha", "salary":"5000", "age":"20" } |
4. Specifying the type of request (POST)
1 | And method post |
5. Checking up Status Code & other Response validations in ‘Then’
1 2 3 | Then status 200 And print 'Response is: ', response And match response == {"name": "Isha","salary": "5000","age": "20","id": "2205"} |
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:
Eclipse Console Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 15:48:17.359 [main] DEBUG c.i.karate.cucumber.CucumberRunner - init test class: class examples.TestRunner 15:48:17.689 [main] DEBUG c.i.karate.cucumber.CucumberRunner - loading feature: /C:/Users/CONKJBI/Documents/workspace-sts-3.9.8.RELEASE/karatedemoproject/target/test-classes/examples%5cpostRequestTest_DummyRestAPI.feature 15:48:19.253 [main] INFO com.intuit.karate.ScriptBridge - karate.env system property was: null Oct 25, 2019 3:48:20 PM org.glassfish.jersey.logging.LoggingInterceptor log SEVERE: 1 * Sending client request on thread main 1 > POST http://dummy.restapiexample.com/api/v1/create 1 > Content-Type: application/json {"name":"Isha","salary":"5000","age":"20"} Oct 25, 2019 3:48:21 PM org.glassfish.jersey.logging.LoggingInterceptor log SEVERE: 1 * Client response received on thread main 1 < 200 1 < Accept-Ranges: bytes 1 < Access-Control-Allow-Origin: * 1 < Access-Control-Expose-Headers: Content-Type, X-Requested-With, X-authentication, X-client 1 < Age: 0 1 < Cache-Control: no-store, no-cache, must-revalidate 1 < Connection: keep-alive 1 < Content-Length: 126 1 < Content-Type: text/html; charset=UTF-8 1 < Date: Fri, 25 Oct 2019 19:48:21 GMT 1 < Expires: Thu, 19 Nov 1981 08:52:00 GMT 1 < PageSpeed: off 1 < Pragma: no-cache 1 < Referrer-Policy: 1 < Response: 200 1 < Server: nginx/1.16.0 1 < Set-Cookie: ezCMPCCS=true; Path=/; Domain=restapiexample.com; Expires=Sun, 25 Oct 2020 19:48:21 GMT,active_template::133674=pub_site.1572032900; Path=/; Domain=restapiexample.com; Expires=Sun, 27 Oct 2019 19:48:20 UTC,ezoab_133674=mod59; Path=/; Domain=restapiexample.com; Expires=Fri, 25 Oct 2019 21:48:20 UTC,ezoref_133674=; Path=/; Domain=restapiexample.com; Expires=Fri, 25 Oct 2019 21:48:20 UTC,ezoadgid_133674=-1; Path=/; Domain=restapiexample.com; Expires=Fri, 25 Oct 2019 20:18:20 UTC,PHPSESSID=f191bab69569a6d80280107c97803452; path=/ 1 < Vary: Accept-Encoding,X-APP-JSON,Accept-Encoding 1 < Via: 1.1 varnish (Varnish/6.0) 1 < X-Ezoic-Cdn: Miss 1 < X-Middleton-Response: 200 1 < X-shard: 100224184 1 < X-Sol: pub_site 1 < X-Varnish: 807969923 1 < X-VCache: Miss {"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Isha' for key 'employee_name_unique'}} 15:48:21.756 [main] DEBUG com.intuit.karate.StepDefs - response time in milliseconds: 1846 15:48:21.815 [main] INFO com.intuit.karate.StepDefs - [print] {error={text=SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Isha' for key 'employee_name_unique'}} 15:48:21.821 [main] ERROR com.intuit.karate.StepDefs - FAILED, path: $.name, actual: null, expected: Isha, reason: not equal Failed scenarios: examples/postRequestTest_DummyRestAPI.feature:3 # Scenario: Verify that a new employee is successfully getting created 1 Scenarios (1 failed) 6 Steps (1 failed, 5 passed) 0m3.978s com.intuit.karate.KarateException: path: $.name, actual: null, expected: Isha, reason: not equal at com.intuit.karate.StepDefs.handleFailure(StepDefs.java:516) at com.intuit.karate.StepDefs.matchNamed(StepDefs.java:491) at com.intuit.karate.StepDefs.matchEquals(StepDefs.java:480) at ✽.And match response == {"name": "Isha","salary": "5000","age": "20","id": "2205"}(examples/postRequestTest_DummyRestAPI.feature:10) |
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 XIf you like this post , please check out my other useful blog posts:
- How to send a POST Request using Rest Assured
- How to make a GET Request using Apache HttpClient in Java
Other Useful References: