Skip to content

Core\Response::sendBytes

Sends a body exactly as it is, with the content type you give. Use it for anything that is not an HTML page, plain text or JSON: an image, a CSV file, a PDF or a calendar file. The other body methods, Core\Response::sendHtml, sendText and sendJson, choose the content type for you. This one needs it as its second argument.

The body is bytes, so it can contain any value, even ones that are not valid text. Nothing is escaped or changed. The content type becomes the Content-Type header. It must not be empty, and every character must be printable ASCII. A line break, a control character or a letter like é throws a LogicError, and nothing is sent.

The examples show an image, a content type that is not allowed, and a CSV report that the browser saves as a file.

Parameters

  • $bodybytesThe bytes to send. Nothing is escaped or changed.
  • $contentTypestringThe content type, such as image/png or text/csv; charset=utf-8. A tainted value does not compile, because the browser obeys this header.

Return value

Returns void. Nothing. Using this and echo in one response does not compile.

Errors

  • LogicError$contentType is empty, or has a character that is not printable ASCII: a line break, a control character or a letter like é. Nothing is sent.

Changelog

VersionChange
0.0.6The Core\Response methods that send a body now start with send: sendJson, sendHtml, sendText, sendBytes and sendSlotted. startStream starts a streamed response. The old names were json, html, text, bytes, slotted and stream.

Examples

An svg image

<?nvs
// Shows how to send a body with a content type that you choose.
// An SVG image is text, but its content type is `image/svg+xml`.
string $circle = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\">"
. "<circle cx=\"10\" cy=\"10\" r=\"8\" fill=\"teal\"/></svg>\n";
// `as bytes` converts the text to bytes. `sendBytes` sends them exactly as they are
// and adds the header `Content-Type: image/svg+xml`.
Core\Response::sendBytes($circle as bytes, 'image/svg+xml');
Output
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><circle cx="10" cy="10" r="8" fill="teal"/></svg>

A content type that is not allowed

<?nvs
// Shows what happens when the content type contains a character that is not allowed.
// Every character of a content type must be printable ASCII. `é` is not,
// so `sendBytes` throws a `LogicError` and sends nothing.
tainted string $answer = '';
try {
Core\Response::sendBytes('Hello' as bytes, 'text/plain; name=café');
$answer = 'sent';
} catch (LogicError $error) {
$answer = $error->message;
}
// This second call uses an allowed content type, so its body is sent.
// The body is the error message from the first call.
Core\Response::sendBytes(($answer . "\n") as bytes, 'text/plain; charset=utf-8');
Output
Core\Response::sendBytes(): `text/plain; name=café` is not a media type a `Content-Type` header can carry — a field value is printable ASCII and never empty

Download a csv report

<?nvs
// Shows how to send a CSV report that the browser saves as a file.
array<string> $orders = ['1042,Ada,19.90', '1043,Grace,5.00', '1044,Alan,42.50'];
string $csv = "order,customer,total\n";
foreach ($orders as string $order) {
$csv = $csv . $order . "\n";
}
// This header tells the browser to save the body as a file named `orders.csv`.
Core\Response::setHeader('Content-Disposition', "attachment; filename=\"orders.csv\"");
// The content type tells the browser that the file is CSV text in UTF-8.
Core\Response::sendBytes($csv as bytes, 'text/csv; charset=utf-8');
Output
order,customer,total
1042,Ada,19.90
1043,Grace,5.00
1044,Alan,42.50