Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script make HTTP POST

I would like to make an HTTP POST using google apps script. I have found very little documentation on this, maybe someone could kindly help?

The HTTP POST will be to a service called 'Twilio':

https://www.twilio.com/docs/api/rest/sending-sms

The page says I only need three parameters in my POST:

  • 'From' number
  • 'To' number
  • 'Body'

I am hoping that the HTTP Post will invoke Twilio to send an sms.

In the end I want to get these parameters from a google spreadsheet. I can do that myself as I'm fairly comfortable with Apps Script but I've never done an HTTP POST, so for now (and for the sake of simplicity) I'll just type these parameters into the code myself.

If anyone knows how to do this, or make any sort of HTTP POST with Apps Script, please let me know - I've spent hours trying to figure this one out!

like image 540
Vin Avatar asked Feb 07 '13 02:02

Vin


2 Answers

i managed to do it, here's the code:

var url = "https://api.twilio.com/2010-04-01/Accounts/ ...account.SID... /SMS/Messages.json";
var options = {
    "method": "post",
    "headers": {
        "Authorization": "Basic " + Utilities.base64Encode(" ...account.SID... : ...auth.token... ")
    },
    "payload": {
        "From": "+12025551212",
        "To": "+14155551212",
        "Body": "Test from Google Apps Script"
    }
};
var response = UrlFetchApp.fetch(url, options);
like image 78
Vin Avatar answered Sep 24 '22 19:09

Vin


Take a look at the UrlFetchApp class. Also you probably need to pass some sort of authentication parameters to the API.

like image 35
Sebastian Kreft Avatar answered Sep 26 '22 19:09

Sebastian Kreft