Angular 2 Http post error

2019-09-18 02:25发布

I have an Ionic 2 app that will get some events from Facebook. I'm trying to send a post request to the Graph Api with a batch containing multiple requests but my subscription always return this error:

{"_body":"","status":404,"statusText":"Ok","headers":{"Client-Via", ["shouldInterceptRequest"]},"type":2,"url":"https://graph.facebook.com/"}

The subscription inside my component:

this.eventListService.get()
  .subscribe(
    response => this.response = response,
    error => this.error = error
  )

EventListService:

import {Injectable} from '../../node_modules/angular2/core';
import {Http, Headers, URLSearchParams} from '../../node_modules/angular2/http';
import {Observable} from '../../node_modules/rxjs/Rx';
import {AuthenticationService} from '../user/authentication.service';

@Injectable()
export class EventListService {
  private fields: string = 'id, cover, name, start_time, place, attending_count, interested_count, rsvp_status';

  constructor(
    private http: Http,
    private authenticationService: AuthenticationService
  ) {}

  get() {
    return Observable
      .fromPromise(this.authenticationService.getAccessToken())
      .flatMap(accessToken => this.synchronize(accessToken));
  }

  synchronize(accessToken) {
    const today = new Date();
    today.setHours(0, 0, 0, 0);
    const todayTimestamp = today.getTime();

    const batch = [{...}];

    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    const body = new URLSearchParams();
    body.append('access_token', accessToken);
    body.append('batch', JSON.stringify(batch));

    return this.http
      .post('https://graph.facebook.com', body.toString(), { headers })
      .map(response => response.json());
  }
}

Any idea why I'm getting this error when making the request?

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-18 03:30

A 404 Error = Not Found (https://en.wikipedia.org/wiki/HTTP_404)

So probably you have the url wrong for whatever resource you are trying to access, or else some other problem with your connection.

Indeed - if I manually try to navigate to https://graph.facebook.com from my browser I get a 404 error.

Since this is using an https protocol, it could be an authentication issue too.

查看更多
登录 后发表回答