Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No overload matches this call. The last overload gave the following error. Type '"text"' is not assignable to type '"json"'

I am trying to GET a HTML page from my API using Angular with JWT tokens in the header. But if I don't specify the text type the compiler spits out a cannot parse error (it tries to parse the HTML as JSON then), so I need to specify my response type.

Here is my component.ts:

  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json',
      'Authorization': 'Bearer TOKENHERE'
    })
  };
  constructor(private http: HttpClient) { }

  ngOnInit() {


  }
  fetchDashboard() {
    this.http.get('http://localhost:3000/avior/dashboard', {responseType: 'text', this.httpOptions,}).subscribe(j => console.log(j));
}
}

I tried removing or changing the content-type to text/html without any avail.


My API Response:

<html><head><title>Dashboard</title></head><body><h1>Dashboard works!</h1></body></html>

like image 224
Munchkin Avatar asked Oct 28 '25 17:10

Munchkin


2 Answers

I did it this way:

    getDivisionName(x: string): Promise<any> {
    return this.httpClient
      .get<any>(this.serviceUrl + `/something/${x}/`, {
        responseType: 'text' as 'json',
      })
      .toPromise();
  }
like image 162
Sanchitos Avatar answered Oct 31 '25 08:10

Sanchitos


You could try the following on in your component to see if your call works to another website. The following should return the html of the requested page as a string. If this does not work with your API then perhaps the problem is not your code in the component, but the response your API is returning

const requestOptions: Object = {
  headers: new HttpHeaders().append('Authorization', 'Bearer <yourtokenhere>'),
  responseType: 'text'
}

this.http.get<string>('https://cors-anywhere.herokuapp.com/https://stackoverflow.com/questions/tagged/angular', 
    requestOptions)
        .subscribe(response => {
               console.log(response);
        }
);
like image 28
Mikey123 Avatar answered Oct 31 '25 08:10

Mikey123



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!