APIs Unleashed 01:
Handling HTTP Methods

API unleashed banner

HTTP (HyperText Transfer Protocol) is the protocol used to send and receive data over the Internet. When you make an API call, an HTTP client sends a request to an HTTP server, which then sends back a response with the requested data to the client.

An HTTP request is an action to be performed on a resource identified by a given Request-URL(baseURL). Request method names are case-sensitive, and should always be denoted in uppercase, i.e GET, POST etc.. There are various HTTP request methods with specific purposes, we use different methods for different API actions.

Postman: Handling HTTP Methods

Let's see how we can handle HTTP methods using POSTMAN

Building HTTP Requests in Postman:

Once you read through the Postman 101 blog, you will now have an idea on how to create Workspaces, Collections and Requests.

 

HTTP Requests are nothing but the action of the API.

 

Once you have your collection ready, you can directly go ahead and add the requests inside it.

postman collection

We will now see how to add different request types under the same collection.

GET request:

GET request is actually used to retrieve/read data from the server.

 

Create new request by selecting the three dots next to the collection name:

Add Request

2. Postman generates the GET request type as default one and with default header values too. You can edit them based on your API documentation if needed

default request type

3. Add the request name and paste the URL “https://reqres.in/api/users/2” in the field next to the request type. This request doesn’t have any payload to be sent as part of “Body”

get request

4. Click “Save” and “Send”

get req response

Tada!! We built our first request. We got the successful response back from the server.

POST request:

It is the request type where need to send data to a server to create or update a resource (at times)

 

  1. Same as above, create a new request and name it. Instead of default “GET” type, change it to “POST” method
post method

2. Paste the URL “https://reqres.in/api/users

post url

3. Since this is a POST request, it requires some Payload to be sent in the “Body” section

For example,

paste the below snippet under “Body

{

"name": "Synapse QA",
"job": "Blogger"

}

4. Click “Save” and “Send”

post req response

We received a successful response back from the server and the user id is 261.

PUT request:

PUT request is used to update the existing resource/entity. Here we will try to update the same record which we created using the POST request recently.

 

  1. Creating a new request and name it, change the request type to “PUT”. And paste the URL “https://reqres.in/api/users/261
put request

2. Now we are filtering the user id “261” in the URL. We also need to pass the updated data as part of the “Body”

3. Click “Save” and “Send”

Now see that the entity is updated as expected.

DELETE request:

The DELETE request method is used to delete/remove the resources mentioned in the URL.

We will delete the same user which we created using the POST request.

 

  1. Creating a new request and name it, change the request type to “DELETE”. And paste the URL “https://reqres.in/api/users/261“. Generally, the “DELETE” will not have any payload
delete request

2. Click “Save” and “Send”

delete response

We received the response 204 error code, where server has successfully fulfilled the request and there is no additional content to send in the response payload body. Some APIs will show message like that the “User is deleted“.

 

We learnt how to build the different HTTP requests and received the response codes/response back from the server.

KarateDSL: Handling HTTP Methods

Let's see how we can handle HTTP methods using KarateDSL

Creating a Karate API Automation Framework

Creating a Karate API Automation Framework is quite simple, you may refer to this blog, if you want to create a Karate API Automation framework from scratch.

In just three easy steps, you may create a new feature file:

To define the test scenario, you’ll need a feature file. We may also add multiple scenarios into single feature file.

Follow the steps below:

 

Step 1: Select “New” from the menu by right-clicking the package.

 

Step 2: Find “File” and click it.

 

Step 3: Add a “.feature” to the end of the filename, such as testFile.feature.

How to run your Karate Feature file(s)?

Runner file is present under the same package with “.java” extension.

 

Refer following code snippet to view runner class:

runner class

For running a single file, pass feature file name in Runner.path method.

 

If you wish to execute all the feature files in a package, use the Runner.path method to pass the package name, as illustrated below:

runner path with package

Curious to see how HTTP methods handled by karateDSL?

If you are keen to learn more about Gherkin Keywords, this blog will be beneficial.

 

Now, let us have a look at each method syntax.

GET Method :

Following code snippet is written to handle GET method using karate.

get response gherkin
get response in KarateDSL

POST Method:

Following code snippet is written to handle POST method using karate. In Post method, request body is passed using “request” keyword.

post response KarateDSL
post response Karate

PUT Method:

Like Post method, request body is also required in Put method. Hence we are using using “request” keyword.

put request KarateDSL
put response KarateDSL

DELETE Method:

Like Delete method, request body is also required in Put method. Hence we are using using “request” keyword.

delete request KarateDSL
delete response

Added benefit: All scenarios in a single feature file. Isn't it wonderful?

The best thing is that we have the ability to define All of the scenarios listed above can be combined into a single feature file.

KarateDSL full feature set

How to view Karate Report for executed feature file(s)?

Once the execution is completed, Karate report are stored under “target” folder. (Please refresh the project if you don’t fine the report)

 

Copy the link generated link and paste it into any browser and hit enter.

karate execution status

You will get to see the latest execution report.

 

1. Single Feature file report.

karate single file report

If you want to see scenario steps in detail of feature, click on the feature file name.

detailed

2. On running multiple feature files present under a package, report look like –

multiple report

Note: We used the demo end points by https://reqres.in/

So now we read about the frequently used HTTP requests types (GET, POST, PUT and DELETE) and how to build them in Postman and KarateDSL. The same way you can try with other request types and practice more. This is the basement for testing your APIs. It will help you to convert the API documentation into API requests, which in turn will help you to understand better about your API capabilities. Hope you have learnt something new in two different tools for today. 
 
Dear readers, thank you for your time. See you in the next few days with our next topic!!

Know Our Synapses

Meet the brains behind this learning series!

Let's Together

Follow Us

Stay updated with all the latest contents & announcements

Share this:

Like this:

Like Loading...

Up ↑

%d