Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular HttpHeaders responseType: 'text' . giving 'Type 'string' is not assignable to type 'json'"

Angular HttpHeaders responseType: 'text' . giving Type 'string' is not assignable to type 'json'. error. I know the response is not JSON. I do not understand how to change the type? I want to take that text (HTML) and parse it with a regex after.

Code

    const httpOptions = {
      headers: new HttpHeaders({
        accept: '*/*',
        contentType: 'application/x-www-form-urlencoded',
      }),
      responseType: 'text',
    };
    post = this.httpClient.post(destinationUrl, httpBody, httpOptions);

    post.subscribe(
      (res) => {
        console.log(res)
      },
      (error) => {
        console.error('download error:', error)
      },
      () => {
        console.log('Completed')
      },
    );

Error from console enter image description here

As you can see, the response type is text. Because of something I don't understand, I cannot make it accept text, as it is waiting for json...

like image 629
IWI Avatar asked Oct 15 '25 07:10

IWI


1 Answers

I solved this issue making HTTPOptions as an Object

let HTTPOptions:Object = {

        headers: new HttpHeaders({
            'Content-Type': 'application/json'
        }),
        responseType: 'text'
     }
    return this.http.post<any>(url, body,  HTTPOptions ).pipe(
        catchError(this.handleError)
    );
like image 89
NITIN VERMA Avatar answered Oct 18 '25 10:10

NITIN VERMA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!