getting a 404 when trying to upload a file to the

2019-05-21 10:50发布

问题:

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

回答1:

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