banibanc
banibanc

Reputation: 141

404 error with .get on angular2

I'm new to angular2 and I've tried to develop my own app based on the angular2 quickstart project.

So the problem is : when I'm trying to use a real API to get my data, I have always a 404 error (no matter what the API url is).

Here is my service code :

import { Injectable }    from '@angular/core';
import { Headers, Http, Response } from '@angular/http';

import { League } from './league';
import { Observable }     from 'rxjs/Observable';

@Injectable()
export class LeagueService {

 private headers = new Headers({'X-Mashape-Key': 'MY_API_KEY'});
 private leaguesUrl = 'https://sportsop-soccer-sports-open-data-v1.p.mashape.com/v1/leagues';

 constructor(private http: Http) {}

 getLeagues(): Observable<League[]> {
    return this.http.get(this.leaguesUrl,{headers: this.headers})
                .map(this.extractData)
                .catch(this.handleError);
 }

 private extractData(res: Response) {
    let body = res.json();
    return body.data || { };
 }

  private handleError (error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }
}

The handleError method sent me back this : "Not Found Collection 'leagues' not found". I've tried with both promise and observable.

I know for a fact that this API is available and doesn't send back a 404.

Could you please tell me what's going on here ? :-)

Thanks !

Upvotes: 0

Views: 586

Answers (1)

Sumit Dhameja
Sumit Dhameja

Reputation: 141

Try adding content type to headers in constructor.

headers.append('Content-Type', 'application/json');

Upvotes: 1

Related Questions