Skip to content

Core\Http\Stream::status

$stream->status(): int

Returns the status code of a streamed reply, such as 200 or 404.

The status code is the three-digit number at the start of every HTTP reply. It tells your program whether the request worked. status() returns it as an int as soon as the start of the reply has arrived. So you can check it before you read the body.

A code such as 404 or 503 does not throw an error. Your program gets the reply and decides what to do with it. You can call status() as often as you want, also after the body was read.

The examples below check the code before reading the body, and show that a 404 is returned like any other reply. The last one tries a second server when the first one replies that it is busy.

Return value

Returns int. The three-digit status code, as an int. A 4xx or 5xx code is returned like any other code. It does not throw an error.

Examples

Checking the status first

<?nvs
// Shows how to check the status code of a streamed reply before you read its body.
// The reply comes from this table, so nothing is sent over a network.
Core\Test::answerHttp('https://files.example.com/report.txt', 200, { body: "sales: 42\n" });
Core\Http\Stream $report = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://files.example.com/report.txt');
// `status()` returns the code as an `int`. This prints "status 200".
echo 'status ', $report->status(), "\n";
if ($report->status() == 200) {
// This prints "sales: 42".
foreach ($report->lines() as tainted string $line) {
echo $line, "\n";
}
}
Output
status 200
sales: 42

An error code is not an error

<?nvs
// Shows that a reply with an error code is returned like any other reply.
// The server does not have this file, so it replies with 404.
Core\Test::answerHttp('https://files.example.com/missing.txt', 404, { body: 'not found' });
// `Core\Http\Client::openStream` does not throw an error for a 404.
Core\Http\Stream $reply = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://files.example.com/missing.txt');
// Your program checks the code itself. This prints "the file does not exist (404)".
if ($reply->status() == 404) {
echo 'the file does not exist (', $reply->status(), ")\n";
}
Output
the file does not exist (404)

Using a mirror when the server is busy

<?nvs
// Shows how to download from a second server when the first one is busy.
// The main server answers 503, which means it is busy. The mirror answers 200.
Core\Test::answerHttp('https://downloads.example.com/prices.csv', 503, { body: '' });
Core\Test::answerHttp('https://mirror.example.com/prices.csv', 200, { body: "apples,1.20\npears,0.95\n" });
array<string> $servers = ['https://downloads.example.com/prices.csv', 'https://mirror.example.com/prices.csv'];
foreach ($servers as string $url) {
Core\Http\Stream $download = Core\Http\Client::openStream(Core\Http\Method::Get, $url);
// A code from 500 to 599 means the server has a problem. The loop tries the next server.
if ($download->status() >= 500) {
echo $url, " is busy\n";
continue;
}
uint $rows = 0;
foreach ($download->lines() as tainted string $line) {
$rows = $rows + 1;
}
// This prints "2 rows from https://mirror.example.com/prices.csv".
echo $rows, ' rows from ', $url, "\n";
break;
}
Output
https://downloads.example.com/prices.csv is busy
2 rows from https://mirror.example.com/prices.csv