Different Ways to Compose a Request Body
There are several ways you can compose a Request Body in Sauce Labs API Testing, ranging from simple to complex.
The included examples use the POST method, but all examples can be applied to other REST methods.
What You'll Need
- A Sauce Labs account (Log in or sign up for a free trial license).
- An existing API Testing Project. For details on how to create one, see API Testing Quickstart.
Use an Existing Body
The first method is when you have an existing Body to paste into the call:
-
In the Composer, add a
POST
component and enter the URL and all of the required fields.- Url (the url of the resource you want to test) - for example
https://en8cqeyt0w8aw.x.pipedream.net/sample/post/request/
- Variable (the name of the variable that contains the response) - for example
payload
- Mode (the response type) - for example
json
- Url (the url of the resource you want to test) - for example
-
Below the POST request, click Add Child Component and then click Request Body.
-
In the Post Body fields, use the following:
- Content-Type (the content-type of the request) - for example
application/json
- Body (the body required in your call) - for example
{
"method": "post",
"url": "http://www.testme.com/api/run/test"
} - Content-Type (the content-type of the request) - for example
-
Click Save Changes.
-
Now you can execute the call and proceed with your test.
Use Variables in the Request Body
The second method to compose a request body is by using variables in the Body.
-
In the Composer, add a
POST
component and enter the URL and all of the required fields.- Url (the url of the resource you want to test) - for example
https://en8cqeyt0w8aw.x.pipedream.net/sample/post/request/
- Variable (the name of the variable that contains the response) - for example
payload
- Mode (the response type) - for example
json
- Url (the url of the resource you want to test) - for example
-
Below the POST request, click Add Child Component and then click Request Body.
-
Select
application/json
as Content-Type and enter the following in the Body field:{
"user": "${user}",
"password": "${password}",
"url": "http://www.testme.com/api/run/test"
}
In the above scenario, user
and password
are not directly passed in the body, but they are variables defined elsewhere: in the input set or stored in the vault.
-
Click Save Changes.
-
Now you can execute the call and proceed with your test.
Use a Variable from Another Call
The next way to compose a Request Body is by using a variable from another call.
-
Add the call to retrieve the variable from. The following is an example of a common scenario in which you need to perform a login for authentication and retrieve the authentication token required for the following call.
- Url (the url of the resource you want to test) - for example
https://258d50a7c4e44cddcc8a7165ceaf08ec.m.pipedream.net/login
- Variable (the name of the variable that contains the response) - for example
payload
- Mode (the response type) - for example
json
- Url (the url of the resource you want to test) - for example
-
Below the request, click Add Child Component and then click Basic Authentication.
-
In the Basic Authentication window, enter the following:
- Username - for example
username
- Password - for example
password
- Username - for example
-
Click Confirm
-
Click Save Changes.
-
The response payload from the login call will contain the access token as follow:
Response Payload Example{
"userid": 4628362,
"access_token": "PXSbs4UscM1lNo6zxdF_yw7MnfVEuHPLtTu3svvKlIZ3Qdf6Qb.7ZujNqFOpsjL4LOpc5oMdbLkbhAfERsEdWyJ7WBlKXIxbitsN3iHo41vfbz9v87cVLvIcqFHID7AczjS51P9c3_wBeaCVMacHkeCVNWAmRcPhtnUEqetAVOCbTxkiLlYJtjTtTq6wTNf4vlIvnK06r7jFRAZselu0oUWePh7wOTZa5EGDbPRwlk7HBK2tnjV4SFaFfJkXNHwM"
} -
Save the token as a variable using a
SET
component.-
Variable (the variable name) - for example
token
-
Mode (the variable type) - for example
String
-
Value (retrieves the value from the previous payload - for example
${payload.access_token}
-
-
Once the token has been saved as a variable, add the second call and use that token in the request body.
- Content-Type - for example
application/json
- Body - for example
{
"token": "${token}"
} - Content-Type - for example
Using an Object from Another Call
Using an object from another call is a more complex method to compose a request body. Scenarios in which you might use this method include when you need to use an object retrieved from a previous call in the body of a subsequent call.
-
Add a
GET
method to perform the call you retrieve the object from.- Url (the url of the resource you want to test) - for example
https://eop8vny94gvdmc5.m.pipedream.net/search
- Variable (the name of the variable that contains the response) - for example
searchPayload
- Mode (the response type) - for example
json
- Url (the url of the resource you want to test) - for example
-
Add a
Query Param
to the call- Name (the name of the param) - for example
search_term
- Value (the value of the param) -for example
sauce
- Name (the name of the param) - for example
The response payload from the call will be:
{
"search_term": "sauce",
"items":[
{
"id":11,
"name":"Mayonnaise"
},
{
"id":12,
"name":"Hollandaise sauce"
},
{
"id":13,
"name":"Tomato ketchup"
}
]
}
- Let's say you need the object
items
as the body in the subsequent call. So, as a second call, add aPOST
and enter the following as body:
{
"items": ${searchPayload.items.asJSON()}
}
- Now you can proceed with your test.
Creating a New Structure to Add as a Body
The last scenario is another complex one. In this case, consider the scenario where you use data from a previous call to create a new structure to add as a body.
- Perform the call that retrieves the data you are using. In the following example, the
GET
returns an array of items.
The response payload:
{
"items":[
{
"id":11,
"price":5.99
},
{
"id":12,
"price":6.99
},
{
"id":13,
"price":10.99
},
{
"id":14,
"price":15.99
}
]
}
-
Create the new data structure by adding a
SET
component.- Variable (the variable name) - for example
itemsAvailable
- Mode (the variable type) - for example
Language
- Language - for example
Javascript
- Body - for example
payload.items.forEach(function(item) {
item.currency = "$";
});
return payload; - Variable (the variable name) - for example
-
Click Save Changes
-
Add the new structure as Request Body in the subsequent
POST
. -
Click Save Changes
-
Now you can proceed with your test.