0

所以几天来我一直在用头撞墙。我正在尝试使用 Javascript 编码 API 调用到 Dropbox 的路径。Dropbox 采用以下网址

https://api.dropbox.com/1/shares/dropbox/<path>

因此,假设我的根文件夹中有一个文件“Getting Started.pdf”.. 访问共享或任何其他 api 调用应该类似于

https://api.dropbox.com/1/shares/dropbox/Getting%20Started.pdf 

但是,我返回了这个错误

{"statusCode":404,"data":"{\"error\": \"Path '/Getting%20Started.pdf' not found\"}"}

删除空格的 url 编码似乎可行,但会导致文件名与其他特殊字符(如 &

https://api.dropbox.com/1/shares/dropbox/some %26 some.txt

任何人都确切地知道如何对 url 进行编码?

[编辑] 这是我的编码功能

p = encodeURIComponent(utf8)
            .replace(/%2F/g, '/')
            .replace(/\)/g, '%29')
            .replace(/\(/g, '%28');
4

1 回答 1

0

Ok so I've answered my own question after a sleepless night .. here's how to correctly encode in JavaScript (also for node.js)

p = encodeURIComponent(p)
            .replace(/%2F/g, '/')
            .replace(/\)/g, '%29')
            .replace(/\(/g, '%28')
            .replace(/!/g,'%21');
    if (p[0] === '/') { p = p.slice(1); }

Also we were adding the path to the body of the request as well as the url .. big mistake. Make sure that the body of the request ONLY contains expected parameters.

于 2013-11-06T18:17:35.623 回答