Sub topic 11
Text, blob and arrayBuffer responses
This chapter assumes you can declare a basic httpResource and read its value(), status() and error() signals. It also assumes you know what a response body and a Content-Type header are.
Why other variants exist
By default, httpResource parses the response body as JSON. That fails for plain text, images, PDFs and binary data. Angular provides three variants for these cases. Each variant takes the same arguments as the main function: a request function and optional options.
| Variant | `value()` type | Typical use |
|---|---|---|
| `httpResource.text` | `string` | Log files, CSV, Markdown, templates |
| `httpResource.blob` | `Blob` | Images, PDFs, downloads |
| `httpResource.arrayBuffer` | `ArrayBuffer` | Binary formats you decode yourself |
Text
httpResource.text skips JSON parsing and gives you the body as a string. The resource is still reactive, so signals read inside the request function trigger new requests.
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
@Component({
selector: 'app-log-viewer',
template: `
<button (click)="file.set('app.log')">App log</button>
<button (click)="file.set('error.log')">Error log</button>
@if (log.isLoading()) {
<p>Loading…</p>
}
<pre>{{ log.value() }}</pre>
`,
})
export class LogViewerComponent {
file = signal('app.log');
log = httpResource.text(() => `/logs/${this.file()}`, { defaultValue: '' });
}The defaultValue option keeps value() a string before the first response arrives.
Blob
A Blob is a browser object that holds file-like data and a MIME type. Use httpResource.blob when the user will view or save the file.
import { Component } from '@angular/core';
import { httpResource } from '@angular/common/http';
@Component({
selector: 'app-report',
template: `
@if (report.hasValue()) {
<p>{{ report.value().type }}, {{ report.value().size }} bytes</p>
<button (click)="save()">Download</button>
}
`,
})
export class ReportComponent {
report = httpResource.blob(() => '/api/report.pdf');
save() {
const blob = this.report.value();
if (!blob) {
return;
}
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'report.pdf';
link.click();
URL.revokeObjectURL(url);
}
}hasValue() narrows the type in the template, so value() is not undefined inside the @if block. Always call URL.revokeObjectURL when you are done with an object URL. Otherwise the browser keeps the blob in memory.
ArrayBuffer
An ArrayBuffer is a fixed-length block of raw bytes. Wrap it in a typed array or DataView to read it.
import { Component, computed } from '@angular/core';
import { httpResource } from '@angular/common/http';
@Component({
selector: 'app-header-check',
template: `<p>PNG file: {{ isPng() }}</p>`,
})
export class HeaderCheckComponent {
image = httpResource.arrayBuffer(() => '/assets/logo.png');
isPng = computed(() => {
const buffer = this.image.value();
if (!buffer) {
return false;
}
const bytes = new Uint8Array(buffer, 0, 4);
return bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47;
});
}The computed signal re-runs whenever the resource delivers a new buffer.
Common mistakes
- Using plain
httpResourcefor a text endpoint. JSON parsing fails, and the resource enters the error state. - Forgetting that
value()isundefineduntil the response arrives. Guard withhasValue(), a check on the value, ordefaultValue. - Not revoking object URLs created from blobs.
Exercise
A component must show the first four bytes of a binary file served at /api/data.bin, and the file URL depends on a signal. Which variant do you use, and what do you return from the request function?
Show answer
Use httpResource.arrayBuffer. Return the URL built from the signal, for example () => /api/${this.name()}.bin`. Then read value() in a computed signal, wrap it in a Uint8Array` and slice the first four bytes.