Day 09: API Workflows - require help on the challenge

This is my code:

// This test logs the number of starships returned in the server response
pm.test("Number of starships returned", function () {
    // Parse the response body as JSON
    const response = pm.response.json();
    
    // Get the count of starships from the response
    const starshipsCount = response.count;
    
    // Log the count of starships
    console.log("Number of starships returned: " + starshipsCount);
    
    // Assert that the count is greater than or equal to 0
    pm.expect(response.starshipsCount).to.be.at.least(0);
});
// Test to verify the status code is 200
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

I added this statement under the Tests tab to display the number of ships returned in the server response, which I subsequently saved. but level of question 3 where we ask to write another script to check the status 200 when I send I get test errors.

I think that i havenโ€™t write a good test script is the raison that I have a false result but Iโ€™m not sure please help me to have a solution to this part:

1 Like

Hey @science-engineer-650 :wave:

Welcome to the Postman Community! :postman:

This would be causing an error due to it being parsed as the count property.

I believe youโ€™re after .length here.

You would also need to target the results array and not the full response body that is returned in the response variable.

@danny-dainton

Count is a key in the API response, so its actually valid in this situation.

Itโ€™s used in this challenge to control a pagination loop. You keep looping until the count is zero.

@science-engineer-650

I suspect the issue is that you are getting a 404 not found.

Therefore it should be failing to parse the response and means none of your code will actually work.

Please check the console logs to see what is actually being sent. Also check the response to see what was returned.

Is the pageNumber being added correctly?

The very first thing you should have in your tests tab is a test to verify a 200 status code.

I didnโ€™t actually physically check that request or the response from it - just made an assumption :grimacing:

The full suite of Postman badges cover about 70+ different individual requests and Iโ€™m not 100% familiar with all of them off the top of my head :smiley:

One of the best things to do here is to always share the full URL to the public workspace and not just static screen shots. It makes it so much more easier to see what you have in the requests. :sweat:

ok thanks for your suggestion. but I have a preocupation please how can I write the instruction that will permite to me to show the number of starships returned in the server response??

I think this is the origin of my testing errors

This will do that for you.

You need to get used to console logging the elements you are interested in.

console.log(response.count);

As mentioned, the issue original posted is not really related to this part of your code.

You are getting a 404 not found error on the request. (Probably related to the page number variable).

Therefore its not returning any Starships and response.count will not exist.

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