httpMutation(): response bodies
Three ways httpMutation() reads a response body: parse to validate or transform it, httpMutation.blob() / .text() for non-JSON bodies, and downloadProgress while it arrives. A fake HttpInterceptorFn stands in for the server, with artificial delays. Source
Validating the response with parse
Each create goes to both endpoints (2 s, an artificial delay). The second answers 201 with an old body shape, { userId, fullName }.
/api/users
- status
- idle
- statusCode
- —
- value
- —
- error
/api/users/outdated
- status
- idle
- statusCode
- —
- value
- —
- error
// `parse` runs on the JSON body before it resolves the call: its return type becomes TOutput.
protected readonly createUser = httpMutation<string, User, HttpErrorResponse | Error>({
request: (name) => ({ method: 'POST', url: '/api/users', body: { name } }),
parse: parseUser,
});A file in the response: httpMutation.blob() + downloadProgress
The fake backend emits progress events every 400 ms (an artificial delay).
- status
- idle
- value
- —
- error
// `.blob()` reads the body as a Blob: TOutput defaults to Blob, so `parse` is optional.
protected readonly exportUsers = httpMutation.blob<void, Blob, HttpErrorResponse>({
request: () => ({ method: 'POST', url: '/api/exports/users', reportProgress: true }),
});@if (exportUsers.downloadProgress(); as p) {
<div class="actions">
<progress [value]="p.loaded" [max]="p.total ?? 100" aria-label="Download progress"></progress>
<span>{{ p.total ? ((p.loaded / p.total) * 100 | number: '1.0-0') + '%' : p.loaded + ' bytes' }}</span>
</div>
}Plain text in the response: httpMutation.text()
The server answers with the plain-text body "2" (artificial 2 s delay).
- status
- idle
- value
- —
- error
// `.text()` reads the body as a string: `parse` is required because TOutput is a number.
protected readonly purgeInactive = httpMutation.text<void, number, HttpErrorResponse>({
request: () => ({ method: 'POST', url: '/api/users/purge-inactive' }),
parse: (raw) => Number(raw),
});