2

i'm building a gmail add on application to read a current email and retrieve the gmail attachment associated with the email. Once I get the blob of the attachment I try to upload it to my API via a multipart/form-data post request. Currently, my request is failing with a 500 while my postman request work just fine.

my back end is giving the following exception: Exception has been thrown by the target of an invocation. Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.

with postman I don't get any error.

Not sure if i'm creating the request in app script currently. Here is my code.

    var blob = 
     Utilities.newBlob(parsedBlob,payload.contentType,payload.name);
    Logger.log('encode url ' + encodedUrl)
    var metadata = {
        'account_ref.id': payload.metaData.account_ref.id,
        'matter_ref.id': payload.metaData.matter_ref.id,
         description: "uploaded from gmail add on.",
         content_type: payload.contentType,
         file_name: 'test.png',
         size: JSON.parse(payload.size).toString(),
    };

    for (var i in metadata) {
        data += "--" + boundary + "\r\n";
        data += "Content-Disposition: form-data; name=\"" + i + "\" 
         \r\n\r\n" + metadata[i] + "\r\n";
    }
    data += "--" + boundary + "\r\n";
    data += "Content-Disposition: form-data; name=\"\"; filename=\"" + 
    blob.getName() + "\"\r\n";
    data += "Content-Type:" + blob.getContentType() + "\r\n\r\n";
    var payload = Utilities.newBlob(data).getBytes()
                                         .concat(blob.getBytes())
                                         .concat(Utilities.newBlob("\r\n--" + boundary + "--\r\n").getBytes());

    var headers = {
        Authorization: Utilities.formatString("Bearer %s", this.oauthService.getAccessToken())
    };

    var options = {
        method: "post",
      contentLength: blob.getBytes().length,
        payload: payload,
        contentType : "multipart/form-data;charset=utf-8; boundary=--" + 
        boundary,
        muteHttpExceptions: true,
        headers: headers,
    };

Anything i'm doing wrong in preparing the payload?

  • To help you create requests to Gmail API, you can refer to the [apps script documentation focusing on the Gmail Services](https://developers.google.com/apps-script/reference/gmail/). Also, for an in-depth guide to access Gmail API, visit the [official documentation](https://developers.google.com/gmail/api/guides/). – MαπμQμαπkγVπ.0 Apr 04 '18 at 10:13
  • @VladimirCastillo, how do you get boundary? – Niko Oct 01 '18 at 15:53

0 Answers0