简体   繁体   中英

What am I doing wrong in this Angular HTTP request?

I'm getting an error from dropbox api:

Error in call to API function "files/download": HTTP header "Dropbox-API-Arg": could not decode input as JSON.

let url = 'https://content.dropboxapi.com/2/files/download'
    let headers = new Headers({ });
    headers.append('Authorization', 'Bearer ...');
    headers.append('Dropbox-API-Arg', '/readme.txt');
    let options = new RequestOptions({ headers: headers });

      this.http
      .get(url, options)
      .subscribe(this.fileContent)

this is the example they put on their api:

curl -X POST https://content.dropboxapi.com/2/files/download \
    --header "Authorization: Bearer <get access token>" \
    --header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Prime_Numbers.txt\"}"

I might have gotten the headers wrong in angular I've never used them before.

Edit: path variable is a string equal to a file path. ie "readme.txt"

Did a quick google and found this: https://www.dropboxforum.com/t5/API-support/HTTP-header-quot-Dropbox-API-Arg-quot-could-not-decode-input-as/td-p/173822

Essentially the format for the variable path isn't in accordance to what the api expects.

I've added the utility function the link provides:

var charsToEncode = /[\u007f-\uffff]/g;

function http_header_safe_json(v) {
  return JSON.stringify(v).replace(charsToEncode,
    function(c) { 
      return '\\u'+('000'+c.charCodeAt(0).toString(16)).slice(-4);
    }
  );
}

EDIT

Your path variable should look like this:

var path = {
  path: '/readme.txt'
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM