In this example, we will see “How to send a GET request to a Rest API endpoint using Karate DSL library?”. If you are starting from scratch, then first go see this article as it will teach you how to set up Karate project in you IDE and configure required dependencies.
Simple example of GET Request using Karate DSL...!!! Share on XIn this tutorial, we are going to cover below topics:
- What is Get Request?
- How to send GET request using Karate?
- Response Validation
Check out: GET REQUEST using Apache HttpClient in JAVA
Let’s begin:
1. What is GET Request?
GET method is one of the most common method of HTTP Protocol which is used to request data from a specific resource.
Some key points of GET requests:
- GET requests parameters remain in the browser history because they have been sent as part of the URL
- GET requests can only be used to retrieve data not to modify
- GET requests can be cached
- GET requests are less secure and should be avoided when trying to retrieve data from a sensitive resource
- GET requests parameter data is limited as there are lengh restrictions
- GET requests can be bookmarked
- GET requests only allow ASCII characters
- GET requests are prone to get hacked easily
2. How to send GET request using Karate?
Before proceeding, lets set up your project by following this article.
Let’s take an example of one of the API GET endpoint available at the above-mentioned website which is ‘/employees’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/employees‘.
At the above resource URL, information about all the employees is present and now we are trying to access those employees details in this below example like id, employee_name, employee_age, employee_salary and profile_image.
Code to send the GET request to the above mentioned Service Endpoint (getRequestTest.feature):
1 2 3 4 5 6 7 8 9 | Feature: Get Employees Scenario: Get the information of all the employees Given url 'http://dummy.restapiexample.com/api/v1/employees' When method get Then status 200 And print 'Response is: ', response And match response contains {"id":"6411","employee_name":"dts44","employee_salary":"3","employee_age":"3","profile_image":""} |
Let’s try to understand the code:
1. Specifying the Feature & Scenario (typical cucumber format)
1 2 3 | Feature: Get Employees Scenario: Get the information of all the employees |
2. Setting up Endpoint URI in ‘Given’
1 | Given url 'http://dummy.restapiexample.com/api/v1/employees' |
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 type of request (GET) in ‘When’ condition
1 | When method get |
4. Checking up Status Code & other Response validations in ‘Then’
1 2 3 | Then status 200 And print 'Response is: ', response And match response contains {"id": "2091","employee_name": "Wadkar Performance","employee_salary": "700000","employee_age": "29","profile_image": ""} |
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 contains {“id”:”6411″,”employee_name”:”dts44″,”employee_salary”:”3″,”employee_age”:”3″,”profile_image”:””} : This line of code helps to check if the specified content is present in the response or not.
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 | [{"id":"2091","employee_name":"Wadkar Performance","employee_salary":"700000"...}] 23:40:08.595 [main] DEBUG com.intuit.karate - response time in milliseconds: 379 23:40:08.607 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $ 23:40:08.613 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $ 23:40:08.615 [main] INFO com.intuit.karate - [print] Response is: [ { "id": "2091", "employee_name": "Wadkar Performance", "employee_salary": "700000", "employee_age": "29", "profile_image": "" }, { "id": "6411", "employee_name": "dts44", "employee_salary": "3", "employee_age": "3", "profile_image": "" }, { "id": "2099", "employee_name": "Sowmya", "employee_salary": "0", "employee_age": "25", "profile_image": "" }, ... ] 23:40:08.621 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $ 23:40:08.621 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $ 23:40:08.622 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $ 1 Scenarios (1 passed) ... |
That’s it, it’s that simple to make a GET method Request using Karate DSL API: ?
Simple example of GET Request using Karate...!!! Share on X
If 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: