尝试将文件上传到Google Drive api时遇到404,并怀疑数据格式问题 [英] getting a 404 when trying to upload a file to the google drive api, suspecting a data formatting issue
问题描述
我试图用JS保存一些动态生成的数据(json)到我的谷歌驱动器上的(新)起初我以为我是POST到错误的地址,但在更改数据内容后,错误从404更改为400,所以现在我怀疑错误是格式相关的(因为我不太了解这个多部分的东西)。
代码如下所示:
pre $ function $ gDriveSaveProject(data,gDriveFolderID,currentProj ,回调)
{
const boundary ='------- 314159265358979323846';
const delimiter =\r\\\
--+ boundary +\r\\\
;
const close_delim =\r\\\
--+ boundary + - ;
$ b var metadata = {
'title':currentProj +.lo,
'mimeType':'application / json',
'parents':[ {'id':gDriveFolderID}]
};
var multipartRequestBody =
分隔符+
'Content-Type:application / json\r\\\
\r\''+
JSON.stringify (元数据)+
分隔符+
'Content-Type:application / json\r\''+
'Content-Transfer-Encoding:base64 \r\\\
'+
'\r\\\
'+
btoa(JSON.stringify(data))+
close_delim;
var request = gapi.client.request({$ b $'path':'/ upload / drive / v2 / files',
'method':'POST',
'params':{'uploadType':'multipart'},
'headers':{
'Content-Type':'multipart / mixed; boundary =''+ boundary +' '
},
//'body'://使用这个函数给我400而不是404
'body':multipartRequestBody
});
request.execute(callback);
}
该代码基于google drive sdk示例,但使用动态创建的数据而不是上传的文件和目标目录而不是根目录。
感谢
也许谷歌可以使它更清晰一点像我这样的傻瓜;)
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.
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).
code is like:
function gDriveSaveProject(data, gDriveFolderID, currentProj, callback )
{
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var metadata = {
'title': currentProj + ".lo",
'mimeType': 'application/json',
'parents' : [{'id' : gDriveFolderID}]
};
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: application/json\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
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);
}
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.
thanks
Figured it out!
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,并怀疑数据格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!