Ionic POST request for upload dropbox

Hi. I’m trying to upload a file to Dropbox using Dropbox Upload API.

But I’m wandering.

Below is my code.

  uploadFile() {
    let headers = new Headers();
    let data = '123';

    this.testToken = <ACCESS-TOKEN>;

    headers.append('Authorization', 'Bearer ' + this.testToken);
    headers.append('Content-Type', 'application/octet-stream');
    headers.append('Dropbox-API-Arg', '{"path":"/0.txt"}');
    return this.http.post('https://content.dropboxapi.com/2/files/upload', data, { headers: headers });
  }

what do I doing wrong? Please guide me.

Solved.

  uploadFile(data: any) {
    let headers = new Headers();
    let mydata: any;
    this.testToken = <ACCESS-TOKEN>;
    headers.append('Authorization', 'Bearer ' + this.testToken);
    headers.append('Content-Type', 'application/octet-stream');
    headers.append('Dropbox-API-Arg', '{"path":"/0.txt"}');
    return this.http.post('https://content.dropboxapi.com/2/files/upload', data, { headers: headers }).subscribe(response => {
      mydata = response.json();
    }, err => {
      alert(err);
    });
  }

But I don’t fully understand why the previous code didn’t work and the current code is perfectly working.

I’m still confused about ‘.subscribe’. Can anybody give me a lesson.

In your first example you create an observable stream (http request), that does not get executed, as long as it has no listener. You are not adding a listener here.

In your second example you subscribe to the observable and add a listener to the stream with that subscription. When the observable has a listener, it gets executed.

You don’t handle the return value, right? In your first example it would be an observable, in your second a subscription

I am write a same code but getting the error…

Error: The request body isn't either a blob or an array buffer
    at Request.Body.blob (http.js:1202)
    at Request.getBody (http.js:2220)
    at Observable._subscribe (http.js:1640)
    at Observable._trySubscribe (Observable.js:172)
    at Observable.subscribe (Observable.js:160)
    at AccountPage.webpackJsonp.110.AccountPage.uploadFile (account.ts:156)
    at AccountPage.webpackJsonp.110.AccountPage.syncDrive (account.ts:144)
    at Object.eval [as handleEvent] (AccountPage.html:50)
    at handleEvent (core.js:13589)
    at callWithDebugContext (core.js:15098)