Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Types of property 'responseType' are incompatible

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"'. 
like image 348
Jitendra Kumar Avatar asked Apr 11 '18 09:04

Jitendra Kumar


1 Answers

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) ); 
like image 92
Titian Cernicova-Dragomir Avatar answered Oct 03 '22 01:10

Titian Cernicova-Dragomir