简体   繁体   English

尝试将文件上传到Google驱动器api时收到404,怀疑是数据格式问题

[英]getting a 404 when trying to upload a file to the google drive api, suspecting a data formatting issue

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. 我试图使用JS将一些动态生成的数据(json)保存到我的Google驱动器上的(新)文件中,但是我在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). 起初,我以为我发布的地址错误,但是更改数据内容后,错误从404更改为400,所以现在我怀疑该错误与格式相关(因为我不太了解这部分内容)。

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. 该代码基于google drive sdk示例,但是使用动态创建的数据而不是上载的文件和目标目录而不是根。

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. 原来我为父文件夹提供了错误的ID,这就是为什么我得到404,但是由于它没有作为正确的错误对象返回,所以我认为404是因为/ upload / v2 / drive / files没有找到。

Maybe google could make it a bit clearer for idiots like me ;) 也许Google可以让像我这样的白痴更清楚了;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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