Day09 get starships request does not respond as expected

Your question may already have an answer on the Postman Community Forum. Please search for related topics to avoid duplication. :pray:

Please read through these guidelines before creating a new topic.

Hello everyone,

Day09 API Workflow request 1- get starships does not return a proper response even if a loop seems to work fine because I have put some breakpoints to be sure to get all starships number that is returned by API…

When after setting pageNumber parameter as mentioned in the documentation, API of starships webpage does not respond with the required page endpoint. This is my code on the tests tab…

And this is the remaining part of the code…

And this what I have on Postman console… The endpoint is not updated as pageNumber is incremented after every loop…

Also after sending request for a second time, there is an endpoint that is “4”

So, please I would like to be sure about the endpoint is not working fine… And my code is ok to send and get the certification…)

The API is working fine.

Please use the preformatted text option in the editor when pasting code. (Don’t use screenshots).

This is one of the more difficult challenges, and its supposed to be a learning exercise so I’m not going to tell you exactly what is wrong (as there are a few things).

What I will say is that my code for this challenge is 28 lines long.

My recommendation is to take a step back and start by writing down all of the steps that you code for first.

Then write the code for those steps.

This will help you when dealing with logic issues.

For example…

// Step 1: Parse response (and console log as appropriate)
// Step 2: Test for status code.
// Step 3: Get current fastest ship.
// Step 4: Loop (For Each Ship)
// Step 4.1: If current ship speed > current fastest ship
// Step 4.2: Update fastest ship details
// Step 5:  Get current page number
// Step 6: If next page is not null
// Step 6.1: Increment current page, etc.
// Step 6.2: setNextRequest()

Hello from Turkey Mike,

I would like to ask what is wrong with my code. Because as I checked your reply about Day09 , all the code starts if there is a next page which means “if next page is not null”. I have put some breakpoints to see where exactly the action is and to check if everything is correct. Please kindly check my code (I know there are many lines because I added some logging statements and some collection variables to see the action breakpoints.)

pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});

const nextPageUrl = pm.response.json().next;

const starships = pm.response.json().results;
console.log(“Total Num of starships:” + starships.length);

pm.collectionVariables.set(“fastestShip”, “Sara”);
pm.collectionVariables.set(“fastestSpeed”, 0);
pm.collectionVariables.set(“pageNumber”, 1);

var jsonData=pm.response.json();

pm.collectionVariables.set(“totalShips”, 0); // Initialize totalShips variable

if (nextPageUrl) {

for (const starship of starships) {

const speed = Number(starship.max_atmosphering_speed);

if (speed > pm.collectionVariables.get(“fastestSpeed”)) {
pm.collectionVariables.set(“fastestShip”, starship.name);
pm.collectionVariables.set(“fastestSpeed”, speed);

let currentPageNumber = pm.collectionVariables.get(“pageNumber”);
currentPageNumber++;
pm.collectionVariables.set(“pageNumber”, currentPageNumber);

let currentStarshipNumber= pm.collectionVariables.get(“totalShips”);
currentStarshipNumber+=starships.length;
pm.collectionVariables.set(“totalShips”, currentStarshipNumber);

console.log(“Fastest Starship in This Page:”, pm.collectionVariables.get(“fastestShip”));

console.log(“Fastest Speed in This Page:”, pm.collectionVariables.get(“fastestSpeed”));

console.log(“Total Num Of Starships:” + currentStarshipNumber);

console.log("Current page Number: " + currentPageNumber)

console.log(“Next Page Available:”, nextPageUrl);

pm.execution.setNextRequest(“get starships”);

} else {
console.log(“No Next Page Available”);
break;
}
}
}

Thanks in advance for your time and help.

Mike Jones via Postman Community <notifications@getpostman.discoursemail.com>, 28 May 2024 Sal, 12:23 tarihinde şunu yazdı:

You haven’t used the preformatted text option in the editor, which prevents all of the code from aligning to the left.

As I mentioned, map out all of the steps and then start from there.

This way, it will be easier to see any logic issues and for anyone reviewing your code to work out what the code is meant to doing.

For example, you have this at the top of your script which will run every single time the request is submitted resetting those variables.

pm.collectionVariables.set(“fastestShip”, “Sara”);
pm.collectionVariables.set(“fastestSpeed”, 0);
pm.collectionVariables.set(“pageNumber”, 1);

The check for next page can go at the bottom of the script as it is only needed to control the setNextRequest() aspect. It doesn’t need to be part of the loop to get the fastest ship.

I am still trying to do my best to get the certificate… I have a very short output on the console. Is this really what we are looking for?

const jsonData = pm.response.json();
var numOfStarships= jsonData.count;
console.log("Total Num of starships:" + numOfStarships);
var starships=jsonData.results;
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
pm.collectionVariables.set("fastestShip", "Sara");
pm.collectionVariables.set("fastestSpeed", 0);
pm.collectionVariables.set("pageNumber", 1)

for (const starship of starships) {
const speed = Number(starship.max_atmosphering_speed);
if (speed > pm.collectionVariables.get("fastestSpeed")) {
pm.collectionVariables.set("fastestShip", starship.name);
pm.collectionVariables.set("fastestSpeed", speed)
}
}
const nextPageUrl = pm.response.json().next;
if (nextPageUrl !== null) {
let currentPageNumber = pm.collectionVariables.get("pageNumber");
currentPageNumber++;
pm.collectionVariables.set("pageNumber", currentPageNumber)
console.log("Fastest Starship :", pm.collectionVariables.get("fastestShip"));
console.log("Fastest Speed :", pm.collectionVariables.get("fastestSpeed"));
} else{ 
pm.execution.setNextRequest("echo the starship"); 
}

This seems to work fine and passing actuall I need to be sure about it...please can you let me know if anything wrong with those codes?

This can’t be right, as every time the request runs, its going to reset these details at the top of your script. This means that your fastest ship will only ever come from the last page of results.

My advice is to write the steps as comments in the code, as it then becomes easier to see the logic.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.