How to get the HTTP status code from a response as a integer in the test script?

Hi Postman community :slight_smile:

My question:
I’m trying to get the HTTP status code from a Response as an Integer. Right now I have:
var status = pm.response.status;
And I have tried a couple other options like responseBody.code; but I haven’t found the right one yet. When I try to parse this is an int, I see that it is not a number. So how do I get the http status as an integer?

Reason why I want this:
The calls in my collection are a bit flaky. In order to check the healthiness of those endpoints first (and fail fast in case they are not healthy) I want to add 2 healthcheck calls to my collection first. Then I want to check the http status code for the healtcheck calls
if it’s 200, fine, continue (using postman.setNextRequest())
if it’s not then I want to wait a bit and retry the healthcheck (while keeping a counter to limit the amount of times the calls will retry)
So this is roughly what I have, but the status is not a number and cannot be compared to 200:

var status = pm.response.status;

if (status == 200) {
    postman.setNextRequest("Next healthcheck call");
} else {
    //...some logic for waiting and retrying the healthchecks
}

Please let me know how to get that HTTP status as a integer or a different way to achieve this. Thank you!

Hi @kesorup,

To cut a long story short, what you’re look for is pm.response.code - this will be a numeric value e.g. 200.

For more details of the response values that are available, you can review the “Scripting with response data” reference documentation:

Thank you Neil! I’ve read that page but somehow still managed to miss it. My collection is working as I wanted now :slight_smile:

1 Like