问题描述
我正在尝试使用 JS 将一些动态生成的数据 (json) 保存到我的谷歌驱动器上的(新)文件中,但我在 POST 中不断收到这些无用的错误.
Im trying to save some dynamically generated data (json) to a (new) file on my google drive using JS but i keep getting these unhelpful errors on my POST.
起初我以为我发布到错误的地址,但在更改数据内容后,错误从 404 更改为 400,所以现在我怀疑错误与格式有关(因为我不太了解这些多部分内容).
At first I thought I was POSTing to the wrong address, but after changing the data content the error changed from 404 to 400 so now i suspect that the error is formatting related (since i dont understand this multipart stuff so well).
代码如下:
function gDriveSaveProject(data, gDriveFolderID, currentProj, callback )
{
const boundary = '-------314159265358979323846';
const delimiter = "
--" + boundary + "
";
const close_delim = "
--" + boundary + "--";
var metadata = {
'title': currentProj + ".lo",
'mimeType': 'application/json',
'parents' : [{'id' : gDriveFolderID}]
};
var multipartRequestBody =
delimiter +
'Content-Type: application/json
' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: application/json
' +
'Content-Transfer-Encoding: base64
' +
'
' +
btoa(JSON.stringify(data)) +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
//'body': "" // using this gives me 400 instead of 404
'body': multipartRequestBody
});
request.execute(callback);
}
代码基于 google drive sdk 示例,但使用动态创建的数据而不是上传的文件和目标目录而不是根目录.
the code is based on the google drive sdk example, but using dynamically created data instead of a uploaded file and a target directory instead of the root.
谢谢
推荐答案
想通了!
结果是我为父文件夹提供了错误的 ID,这就是为什么我得到 404,但由于它没有作为正确的错误对象返回,我认为 404 是因为/upload/v2/drive/files 未找到.
Turned out that I was supplying a faulty ID for the parent folder, and that is why i got the 404, but since it wasnt returned as a proper error object, I assumed the 404 was because the /upload/v2/drive/files wasnt found.
也许谷歌可以让像我这样的白痴更清楚;)
Maybe google could make it a bit clearer for idiots like me ;)
这篇关于尝试将文件上传到 google drive api 时收到 404,怀疑是数据格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!