Dealing with a http.get who sometimes returns a json and other an html body

I’m dealing with an http.get who sometimes returns a json (when the user is logged in) and others returns a html body, i’m adding this imports:

import {Http, RequestOptions, Headers} from '@angular/http';
import 'rxjs/add/operator/map';

      .map(res => res.json()).subscribe(data => {

How can i do than if i receive a html (or another kind of response) throws an error?

Wow, that’s a stupendously bad API. That being said, you can’t blindly call json() on the response as you are doing now. Try checking the headers property of it first, and hope that the Content-Type is at least properly set.

Hummm… Have some code there? Im a bit lost

Hmm I woud try

      .map(res => res.text()).subscribe(data => {
       json = JSON.stringify(data);
    } catch (ex) {
        // No json 


Haven’t tried it just a guess.