In order for Angular’s HttpClient
to process plaintext responses and not result in an error, you need to set responseType: 'text'
in the options
(which is the second parameter to .get()
. Otherwise, Angular will try to parse the plaintext response, even if the response MIME type is set to text/plain
.
getPlaintext(command: string): Observable<string> { return this.http.get(`/api/text`, { responseType: 'text' }); }
Full service example
import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class PlaintextService { constructor(private http: HttpClient) { } getPlaintext(command: string): Observable<string> { return this.http.get(`/api/text`, { responseType: 'text' }); } }