最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getting a 404 when trying to upload a file to the google drive api, suspecting a data formatting issue - Stack Over

programmeradmin0浏览0评论

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

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

Share Improve this question asked Jun 13, 2013 at 10:21 laserpandalaserpanda 3871 gold badge3 silver badges10 bronze badges 2
  • Could you provide further debugging data? I would like to see request data made from this function. One possible reason I can think of is that base64 is not url safe and there might be slash in the data which is misleading url parser and causing 404 error. – JunYoung Gwak Commented Jun 13, 2013 at 16:35
  • Thanks, but I solved it. See below. PS I love those mangoes you have as a profile pic! ;) – laserpanda Commented Jun 13, 2013 at 19:43
Add a ment  | 

1 Answer 1

Reset to default 5

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 ;)

发布评论

评论列表(0)

  1. 暂无评论