What is API Payload?
In API(Application Programming Interface), a ‘Payload’ refers to the dataset which is sent within request or received through a response. When we request data from a server, the data we send along with the request or the data we receive in response is the payload. It's like the content of a letter we're sending or receiving.The payload can be sent or received in various formats, including JSON (JavaScript Object Notation). Usually, the payload is denoted using the “{}” in a query string.
Types of API Payload:
Key Components of API Request:
An API request consists of several components that convey specific information to the server. Below are the components which help to fulfill the requirements for both client and server.
API EndPoints : The API endpoint is the resource URL that we want to access.
HTTP methods: HTTP methods include below
GET: This is read only value by this method we can get the resource's data
POST: With this method we are creating a new resource in the server
PUT: With this method we can update the resource with new data in the server
Delete: With this method we can delete the resource from server
Request Headers: Header information which are sent with API request, below are few examples
Authorization which is access token to access the endpoints
The expected response format (application/json to request JSON data)
Request Payload: Request payload is basically the data set which we are sending in to the server in the request body to get the response
Query Parameters: There are few additional data need to sent with the URL to modify the request or filter the response.
Response Headers: Response headers includes few additional information about the server’s response
Response body: The response payload body contains the data returned by the server in response to the API request. Response payload formats include JSON, XML, HTML, or plain text.
200: When the request was successful, the server returned the requested data as 200 OK
204: The request was successful, but there is no data to return. Then it shows as 204 NoContent. It is used for successful DELETE requests.
400: The server cannot process the request due to a client side error. Then it display 400 Bad Request
500: The server encountered an error while processing the request, indicating a server-side issue and gives Internal Server Error
What is Request Payload?
The request payload is the data set in the request body. The data sent to the server for methods like POST, PUT, or PATCH.
JSON payload format can be different types:
As a simple JSON format
As a Complex Nested JSON format
** This article gives a detailed overview to create Simple JSON Request payload body as well as Complex Nested JSON Request payload body.
[Creation of a Simple JSON Request Payload]
Below examples are shown to share 4 different ways of Simple JSON Request Payload creation:
Using HashMap
Using Org.JSON
Using POJO
Using External JSON file
Simple JSON Request Payload:
{
"name": "Jhon",
"job": "Associate"
}
In this above example, we're sending the payload object as JSON in the body of a POST request to an API endpoint for creating user with the Job role.
Scenario: Create User (with valid endpoint and valid data. http method used as POST)
Step1: Create a Maven Project
Step 2: Add all required dependencies in POM.xml file
Step3: Create Feature File for create user scenario under src/test/resources
Step4: Create EndPoint enum class to maintain the endpoint details under src/test/java/utilities
Step5: Under src/test/java/stepDefinitions create stepdefinition class for createUser
Step6: Create TestRunner Under src/test/java/cucumber.Options
Using HashMap:
Follow the below process to create the request payload:
@Given section we have to create the request payload using HashMap key value pair method
First need to create the request body as per the JSON format using HashMap put operation
Now create the request, using body (created above), header(contentType) & query-parameter information and store into a request variable
@When section we have to pass the endpoint and httpmethod to the request payload and need to store into a response variable ·
@Then section after getting the response we can validate the status code
Using Org.JSON:
Follow the below process to create the request payload:
@Given section we have to create the request payload using org.JSON key value pair method
First, we have to add JSON org library in pom.xml file
Then need to create the request body as per the JSON format using JSONObject put operation and also need to convert the JSONObJect data to string
After the above create the request, using body (created above), header(contentType) & query-parameter information and store into a request variable
@When section we have to pass the endpoint and httpmethod to the request payload and need to store into a response variable ·
@Then section after getting the response we can validate the status code
Using POJO(Plain old java object class ):
Follow the below process to create the request payload:
Create createuser POJO class under src/test/java/POJO
@Given section we have to create first the object of the above POJO class
Then create the request payload body using setter method of POJO class
After the above create the request, using body (created above), header(contentType) & query-parameter information and store into a request variable
@When section we have to pass the endpoint and httpmethod to the request payload and need to store into a response variable
@Then section after getting the response we can validate the status code
External JSON File:
Follow the below process to create the request payload:
Add the external JSON file(payloadbody.json) in your framework
{
"name": "Jhon",
"job": "Associate"
}
@Given section we have to create the request payload from provided external JSON file
First, we have to open the JSON file using java File type object giving the file location as parameter
Then we have to read the file using two classes passing the file object
Java FileReader
JSONTokener from org.json
Using JSONTokener we have to extract the data in a JSON format and for that we have to use JSONObject class
After getting the data in a JSON format now we can pass data in a request body
Then create the request, using body (created above), header(contentType) & query-parameter information and store into a request variable ·
@When section we have to pass the endpoint and httpmethod to the request payload and need to store into a response variable ·
@Then section after getting the response we can validate the status code
[Creation of a Complex Nested JSON Request Payload]
We can create complex nested JSON payload using all 4 different ways as like shown in simple JSON Request payload creation.
Below example to show one way of Complex Nested JSON Request Payload creation:
Using POJO
Complex Nested JSON Request Payload:
{
"id": 0,
"category": {
"id": 0,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
In this above example, we're sending the payload object as JSON in the body of a POST request to an API endpoint for creating Pet information in the Store.
In the above JSON request body, it has:
Nested JSON [category is the nested JSON]
Array in JSON [photoUrls is simple Array]
Nested JSON under Array [tags is the JSON under Array]
To create the above JSON payload, we have to create below POJO classes:
One Parent POJO class
Two child POJO classes [category, tags]
For simple Array mention return type as List of String
For JSON under array mention return type as List of the POJO class of that JSON
Parent POJO class:
Child POJO classes:
Below is the feature file for the complex JSON payload creation
Below are the steps need to mention in the step definition:
@Given section we have to create first the object of the above POJO classes for both parent & child POJO classes
Then create the request payload body using setter method
Use ArrayList to add the data into the array
After the above create the request, using body (created above), header(contentType) & query-parameter information and store into a request variable
@When section we have to pass the endpoint and httpmethod to the request payload and need to store into a response variable
@Then section after getting the response we can validate the status code
Below is the response payload:
Body:
{
"id": 0,
"category": {
"id": 55,
"name": "PetCategory"
},
"name": "MyDog",
"photoUrls": [
"DogPhoto"
],
"tags": [
{
"id": 66,
"name": "DogTags"
}
],
"status": "Available"
}
Very Informative.
It has more info about payload👍🏼
Helpful information