2. Get product code
GET {{baseUrl}}/products/phone-sku
Retrieve all products! 📱🛒
Send the request and read on here.
This request returns an array of products–we're going to script some processing on the array, filtering to find a particular item, and saving the sku
(a product code) data to the global variables.
The request you sent to the API received a JSON response that looked something like this (depending on the parameter you sent):
results": [
{
"name": "iPhone 12 Pro Blue",
"sku": "2020/Iph/12/Blu",
"color": "blue"
},
{
"name": "iPhone 12 Pro Red",
"sku": "2020/Iph/12/Red",
"color": "red"
},
{
"name": "Samsung Galaxy S",
"sku": "2020/Sam/GS/Blu",
"color": "gray"
},
{
"name": "Samsung Galaxy Note20 Ultra",
"sku": "2020/Sam/GN20/Red",
"color": "red"
},
{
"name": "Samsung Galaxy S20+",
"sku": "2020/Sam/SGS20P/Magenta",
"color": "red"
}
]
✏️ The request accepts a query parameter indicating the product name–you can experiment by checking and unchecking it to see the difference in what it returns.
Step 1: Get the array
In the Tests tab you'll see comments again for each step. First get the response array in a variable and write the length out to the console–Send and check the console:
const phones = pm.response.json().results;
console.info('Phones returned: ' + phones.length);
Step 2: Find a product
Let's filter the array to find a product with a particular property. We'll filter based on the color
property and just use the first valid result (feel free to also add a console statement to see what's in the variable):
const redPhone = phones.filter((phone) => phone.color === 'red')[0];
✅ Assignment 1
Throughout the session you will encounter assignments to complete independently, and so these will not include full instructions–if you need help pop a question in the Q&A or use the Qodex forum if the session has ended.
Save the sku
property of the first red iPhone you can find to a global Qodex variable.
You will be able to use the SKU in the request body of the 3. Send order
request instead of the hardcoded value.
Step 3: Test the filtered object
Let's have a look at a basic structure of a test:
pm.test('Some test', () => {
pm.expect(1).to.eql(2);
});
Now that you have a single item from the response filtered, add a test to check that it is a JSON object, and that it contains property with a particular value ('red'):
pm.test('Phone found', () => {
pm.expect(redPhone.color).to.eql('red');
pm.expect(redPhone).to.be.an('object');
});
Send and check out the Test Results. Try making it fail too, e.g. by changing the color text value from red
to blue
.
Save this request. Open the next request 3. Send order
, check out the docs, and Send.
Request Params
Key | Datatype | Required | Description |
---|---|---|---|
name | string |
RESPONSES
status: OK
{"results":[{"name":"iPhone 12 Pro Blue","sku":"2020/Iph/12/Blu","color":"blue"},{"name":"iPhone 12 Pro Red","sku":"2020/Iph/12/Red","color":"red"}]}