angular/angular · error · Error

Automatic conversion to text is not supported for Blobs.

Error message

Automatic conversion to text is not supported for Blobs.

What it means

Thrown by the HttpClient testing backend when a request with `responseType: 'text'` is flushed with a `Blob`. `_toTextBody()` rejects Blobs (and ArrayBuffers) because it cannot decode binary content to a string without charset knowledge; only strings pass through, and other primitive/object values are JSON.stringify'd.

Source

Thrown at packages/common/http/testing/src/request.ts:223

    return body;
  }
  throw new Error(`Automatic conversion to ${format} is not supported for response type.`);
}

/**
 * Helper function to convert a response body to a string.
 */
function _toTextBody(
  body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[],
): string {
  if (typeof body === 'string') {
    return body;
  }
  if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
    throw new Error('Automatic conversion to text is not supported for ArrayBuffers.');
  }
  if (typeof Blob !== 'undefined' && body instanceof Blob) {
    throw new Error('Automatic conversion to text is not supported for Blobs.');
  }
  return JSON.stringify(_toJsonBody(body, 'text'));
}

/**
 * Convert a response body to the requested type.
 */
function _maybeConvertBody(
  responseType: string,
  body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[] | null,
): ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[] | null {
  if (body === null) {
    return null;
  }
  switch (responseType) {
    case 'arraybuffer':
      return _toArrayBufferBody(body);
    case 'blob':

View on GitHub (pinned to 51cb07e980)

Solutions

  1. Flush a string: `req.flush('line1')`
  2. If the consumer really reads a Blob, change the request to `responseType: 'blob'`

Example fix

// before
httpClient.get('/log', {responseType: 'text'}).subscribe();
req.flush(new Blob(['line1'])); // throws

// after
req.flush('line1');
Defensive patterns

Strategy: validation

Validate before calling

if (req.request.responseType === 'text' && body instanceof Blob) {
  throw new Error('Flush a string for responseType text');
}
req.flush(body as any);

Type guard

const isTextFlushBody = (b: unknown): b is string => typeof b === 'string';

Prevention

When it happens

Trigger: In a spec: `httpClient.get('/log', {responseType: 'text'})` then `req.flush(new Blob(['line1']))`.

Common situations: Reusing Blob fixtures in text-response tests; simulating text downloads as binary.

Related errors


AI-assisted analysis of angular/angular@51cb07e980 (2026-08-22). Data as JSON: /api/errors/8a57212cb485eb0a. Report an issue: GitHub.