I am unable to make a POST request in Angular 5 that accepts text/plain
as response. Angular is calling a method which expects JSON
as response and hence throws an error when response comes while trying to parse the response.
I tried calling method with parameter {responseType: 'text'}
but VS code is showing it as an error and I am also getting an error in the console when the application is compiled.
Below is the Angular 5 code for a POST request that expects a response as text/plain
.
this.http .post<string>(this.loginUrl, this.user, {responseType: 'text'}) // error in this line .subscribe( (data) => this.success(data), (error) => this.failure(error) );
While compilation the following error show in the console:
ERROR in src/app/login/login.component.ts(47,47): error TS2345: Argument of type '{ responseType: "text"; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'responseType' are incompatible. Type '"text"' is not assignable to type '"json"'.
The version of post
accepting responseType:'text'
is not generic, since the result type is already clear (ie string
)
this.http .post(this.loginUrl, this.user, {responseType: 'text'}) // no generic parameter .subscribe( (data) => this.success(data), // data is string (error) => this.failure(error) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With