HTTP request to Firebase with Google Apps Script

652 views
Skip to first unread message

Blueprinter

unread,
Feb 24, 2014, 1:53:14 AM2/24/14
to fireba...@googlegroups.com
Google Apps Script has the capability to request a HTTP 'post', 'get', 'put', 'delete', etc.  Can I read and write to Firebase this way?  I've looked at the Firebase documentation for REST Api, and I do not understand it.  curl is a command line tool for getting and sending files.  Is the only way to use a HTTP PUT to Firebase using cURL?  Or can I use a programing languages built in HTTP Requests?   The Firebase documentation has this example:

PUT - Writing Data

Writing data with a PUT request:

curl -X PUT -d '{ "first": "Jack", "last": "Sparrow" }' \
  https://SampleChat.firebaseIO-demo.com/users/jack/name.json

I can issue a PUT request with Google Apps Script:

UrlFetchApp.fetch("http://example.com/file.ext", "method" : "put", "payload" : {"Payload String"});

But the curl command has -X and -d

-d, --data <data>

So I guess -d indicates that the data follows.

With Google Apps Script
UrlFetchApp.fetch, there is a "payload" parameter. There example shows:

   var payload =
   
{
     
"fieldOne" : "value for field one",
     
"fieldTwo" : "value for field two",
     
"fileAttachment": fileBlob
   
};

Maybe the "Payload" converts to the -d cURL parameter? I don't know?

-X, --request <command>

http://curl.haxx.se/docs/manpage.html

So, -X is the type of request? I'm looking at some cURL documentation, and I guess that's what the -X is for,
the PUT, PUSH, GET, DELETE type of request.

So maybe I can use this. It would probably be something like:

UrlFetchApp.fetch("https://SampleChat.firebaseIO-demo.com/users/jack/name.json", "method" : "put", "payload" : '{ "first": "Jack", "last": "Sparrow" }');

Okay, I just opened up the firebase SampleChat database. Maybe I'll try to test write the previous line of code and see what happens.

Blueprinter

unread,
Feb 24, 2014, 12:29:10 PM2/24/14
to fireba...@googlegroups.com
I got it to work!!  Awesome!   I can write to firebase with Google Apps Script!

Here is the code I used in the backend, .gs file: 

 function putToFire() {
  
    var payload = '{ "first": "Mother", "last": "Terresa" }';
    var options =
    {
     "method" : "put",
     "payload" : payload
     };
 
 
    UrlFetchApp.fetch("https://SampleChat.firebaseIO-demo.com/users/fred/name.json", options );
 
    Logger.log(options);

   }


So I can write to firebase with Google Apps Script from the server side .gs file.  The tricky part was getting the text string correct.

Matthew Head

unread,
Apr 8, 2014, 3:38:33 PM4/8/14
to fireba...@googlegroups.com
awesome. thanks for the post!
Reply all
Reply to author
Forward
0 new messages