D
D
danilx5562021-07-06 19:26:35
Google Apps Script
danilx556, 2021-07-06 19:26:35

How to handle POST requests in Google App Script?

I want to receive a webhook from a third-party service and process it in App Script.

Documentation of the service that sends the webhook https://leelooai.atlassian.net/wiki/spaces/DOC/pag...

Wrote a script and published it as an application:

function doPost(e){
  Logger.log(e.postData.contents);
  return ContentService.createTextOutput('response status 200')
}


Pasted the app link and started sending webhooks from a third party service.

But I met a problem - webhooks are received, but they are empty, there is nothing in them:
60e482835e21a689013008.png
Records are visible, but they are not clickable, you can not expand and see the logs...

I tried to display different values ​​in the log:
e, e.postData.getDataAsString() - unsuccessfully .

I decided to try to accept webhooks in a third-party service - everything works there:

Here is such a webhook comes:
{
  "method": "POST",
  "path": "/",
  "query": {},
  "headers": {
    "x-forwarded-for": "18.159.207.230",
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "host": "a33172629dd88415897962d6f803923a.m.pipedream.net",
    "x-amzn-trace-id": "Root=1-60e47bc7-0fbad7b9443e153908bda2f2",
    "content-length": "668",
    "content-type": "application/json"
  },
  "bodyRaw": "{\"events\":[{\"type\":\"SUBSCRIBE\",\"data\":{\"tunnel_id\":\"60d4449f5dfa16280ee1eefd\",\"account_id\":\"602cab53d3959200129b4eec\",\"person_id\":\"602cab53d3959200129b4eeb\",\"leadgentool_id\":\"60d4449f5dfa166e72e1efb9\",\"connection_id\":\"5cc2e9f1db0646000dbe0011\",\"traffic_source_id\":\"60d4449f5dfa1668a7e1efbb\",\"utm_marks\":{\"utm_source\":\"param1\",\"utm_medium\":\"param2\",\"utm_campaign\":\"param3\"},\"email\":\"[email protected]\",\"phone\":\"+79----------25\",\"created_at\":\"2021-07-06T15:50:12.618Z\",\"tunnel_name\":\"А----------ня\",\"account_name\":\"----------\",\"leadgentool_name\":\"System lgt\",\"connection_name\":\"----------\",\"traffic_source_name\":\"Manual\"}}]}",
  "body": {
    "events": [
      {
        "type": "SUBSCRIBE",
        "data": {
          "tunnel_id": "60d4449f5d----------eefd",
          "account_id": "602cab53d----------4eec",
          "person_id": "602cab53d39----------b4eeb",
          "leadgentool_id": "60d4449f5df----------e1efb9",
          "connection_id": "5cc2e9f1db064----------e0011",
          "traffic_source_id": "60d4449f5dfa----------1efbb",
          "utm_marks": {
            "utm_source": "param1",
            "utm_medium": "param2",
            "utm_campaign": "param3"
          },
          "email": "[email protected]",
          "phone": "+79----------5125",
          "created_at": "2021-07----------2.618Z",
          "tunnel_name": "А----------2 июня",
          "account_name": "Данил",
          "leadgentool_name": "System lgt",
          "connection_name": "----------",
          "traffic_source_name": "Manual"
        }
      }
    ]
  }
}


This means that the webhook is sent correctly, but I somehow process it incorrectly in the app script.

Please tell me how to handle this request.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2021-07-06
@danilx556

If you do not go deep, in a nutshell: Logger.log() does not work there. I use something like this:

const logger = {
  log:(val)=>{
    SpreadsheetApp.openById(``) // тут ID таблицы
                  .getSheetByName(`log`) // тут имя листа куда сыпятся логи
                  .appendRow([new Date(), `${val}`]);
  }
};

// Использовать так же, только с маленькой буквы
logger.log(`что-то важное`);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question