Search code examples
restnode.jsgoogle-docsgoogle-docs-api

Google Docs API: cannot set document title


I am trying to upload a file using Node and Google Docs REST API. I can upload the file just fine if I don't include the metadata, but it will always be uploaded as 'Untitled'.

But when I include the meta data I get the following error after sending my atom data and attempting to continue with the file upload:

ParseException - Content is not allowed in prolog

This is my first request to create an upload session and get a resumable-media-link

var meta = '<?xml version="1.0" encoding="UTF-8"?>'
    meta+= '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007">'
    meta+= '<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/docs/2007#document"/>'
    meta+= '<title>Test</title></entry>'

var options = {
    host: 'docs.google.com',
    path: '/feeds/upload/create-session/default/private/full',
    method: 'POST',
    headers: { 
      'Host' : 'docs.google.com',
      'Content-Length' : meta.length,
      'Content-Type': 'application/atom+xml',
      'GData-Version' : 3,
      'Authorization' : 'GoogleLogin auth=' + authToken,
      'X-Upload-Content-Type' : 'application/msword',
      'X-Upload-Content-Length' : 31232
    }
}

var req = https.request(options, function (res) {
    // make 2nd request

});

req.end(meta);

This is what my 2nd request looks like after getting the resumable-media-link

var options = { 
    host: 'docs.google.com',
    path: resumableMediaLink,
    method: 'PUT',
    headers: {
        'Content-Length': data.length,
        'Content-Type': 'application/msword',
        'Content-Range': 'bytes 0-' + (data.length-1) +'/'+ data.length
    }   
}   

var req = https.request(options, function (res) {
    res.on('data', function (chunk) {
         // ...
    }); 
}); 

req.write(data);
req.end();

It seems like I am sending the atom data incorrectly. Any ideas of what I could be doing wrong?


Solution

  • I figured out what I was doing wrong.

    I needed to set the 'Slug' header in the first POST request to initiate a resumable session.

    I had it in the following request.