shouldInterceptRequest : cordova-plugin-whitelist

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 batch = [{...}];

    const headers = new Headers();
    headers.append('Content-Type', 'application/json');

    const body = `access_token=${accessToken}&batch=${JSON.stringify(batch)}`;

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

This angular 1 code works on my web app:

$http.post('https://graph.facebook.com', {
  access_token: authenticationService.getAccessToken(),
  batch: batch,
})

I was looking around and found this topic on StackOverflow. Is something related? What I can’t make this request?

I just ran into this as well, not against Facebook but my own API service. Any solutions?

Edit: it works fine on the browser, just seems to be an Android thing. I have an ionic 1.0 app that hits the same API so I know it’s client side.

Thanks!
Brian

Actually I followed the instructions here and it worked: http://www.gajotres.net/ionic-2-making-rest-http-requests-like-a-pro/

It did require the cordova whitelist plugin and the new line added to index.html and then it worked like a charm

2 Likes

I was just had to install the whitelist plugin. Didn’t came installed with the project from git somehow:
ionic plugin install cordova-plugin-whitelist