Skip to content
Advertisement

Google Apps Script To POST PDF and IMAGE data to external Apache server

I need a way to get image files and pdf files out of our google drive and over to our apache server programmatically.

My PHP works great if I use a standard HTML5 form to perform the upload, which leads me to believe that it is correctly written. (I may be wrong). When I use the google apps script to perform the upload I get the following error.

[06-Aug-2016 16:52:18 America/Chicago] PHP Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

If I change the content type parameter to the following

“contentType” : “multipart/form-data boundary=–“,

This makes the warning go away but no file seems to make it to the destination server. I am fairly certain from my own research that I need to set this boundary paramter for everything to play nicely. I also believe that the default is the double hyphen. But it doesn’t seem to be correct in this case.

any help is appreciated I am a little bit out of my depth here. If more information is needed please let me know and I will respond as quick as humanly possible.

Thank you,

Chris Pfannkuch – Oregon Gutter

///////////////////////////////////////////////////////////////////
// Google Apps Script - Code.gs
function sendHttpPost() 
{
   var fileBlob = DriveApp.getFileById("FILEIDHERE").getBlob();

   var payload =
   {
     "fileAttachment": fileBlob
   };

   // FROM Google Apps Script Documentation.
   // Because payload is a JavaScript object, it will be interpreted as
   // an HTML form. (We do not need to specify contentType; it will
   // automatically default to either 'application/x-www-form-urlencoded'
   // or 'multipart/form-data')

   // FROM PHP.net documentation.
   // Be sure your file upload form has attribute enctype="multipart/form-data"
   // otherwise the file upload will not work.                 

  // When I have omitted this parameter I get multiple warnings in my error log
  // on my destination server.

   var options =
   {
     "method" : "post",
     "contentType" : "multipart/form-data",
     "payload" : payload
   };

   UrlFetchApp.fetch("http://OURDOMAIN.com/SOMEFOLDER/handleHttpPost.php", options);
}
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//PHP - handleHttpPost.php
<?php
    $uploaddir = 'SOMEDIR/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    echo '<pre>';
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
    {
        echo "File is valid, and was successfully uploaded.n";
    } 
    else 
    {
        echo "Failed.n";
    }

    echo 'Here is some more debugging info:';
    print_r($_FILES);

    echo "</pre>";
?>
///////////////////////////////////////////////////////////////////

EDIT: Here is some post data that I have been looking through.

///////////////////////////////////////////////////////////////////
// POST From standard HTML5 WebForm, works great to upload the pdf file.
http://posttestserver.com/files/2016/08/08/f_12.45.381108844561
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
// POST From UrlFetchApp call.
http://www.posttestserver.com/data/2016/08/08/12.56.43188879461
///////////////////////////////////////////////////////////////////

The UrlFetch Call does not seem to see the file as an uploaded file.

== Multipart File upload. ==
Received 0 file(s)

When I use the form it does see the file.

== Multipart File upload. ==
Received 1 file(s)
 0: posted name=fileAttachment
    name: OfficeClerk.pdf
    type: application/pdf
    error: 0
    size: 752176

Advertisement

Answer

SOLVED my issue today. I needed to include the seemingly useless fieldOne, and fieldTwo. Once I did it all started working nicely together. I am an idiot for having removed them at some point in my early debugging process.

var payload =
   {
     "fieldOne" : "value for field one",
     "fieldTwo" : "value for field two",
     "fileAttachment": fileBlob
   };
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement