Search code examples
angularhttp-headersangular-httpclient

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...


Solution

  • 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)
        );