Home > Net >  Google Apps Script web app response for POST request acts weird
Google Apps Script web app response for POST request acts weird

Time:11-25

I'm making a small Apps Script to create a calendar event via POST request.

Here is the code:

function doPost(e) {
    const accessKey = 'l056SH7REYsuli**************************************************DIX0e08XvsBAtzA2eSJg';
    let result;
    let params = JSON.parse(e.postData.contents);
    let event = params.event;
    let calendarId = params.calendarId;
    let token = params.token;
    let method = params.method;
    if(token === accessKey) {
        try {
            eventResult = Calendar.Events.insert(event, calendarId);
            result = { 'Event ID: ' : eventResult.id }
            } catch (err) {
            result = { 
                'Failed with error %s': err.message,
                'Event contents': event
                    }
            }
    } else {
        result = {
            'status': 'Forbidden',
            'statusCode': 403,
            'message': 'You do not have access to this resource.'
        }
    }
    return ContentService.createTextOutput(JSON.stringify(result))
}

It works perfect with Postman: enter image description here

But when I do curl: curl --location --request POST 'https://script.google.com/macros/s/AKfycbyh7n3YeE-HiNAIA8wi9HAVsaLBUv5ceJu-k7yxL4D8mSm9EXQ4wQc_ctqipFlAR4SqfA/exec'
--header 'Content-Type: application/json'
--data-raw '{ "token": "l056SH7REYs************3IYXeiECDIX0e08XvsBAtzA2eSJg", "calendarId": "c_9bae2a34a108dd90[email protected]", "event": { "summary":"TEst from postman", "description":"Abyrvalg", "start": { "date": "2022-11-28" }, "end": { "date": "2022-11-28" }, "colorID": 9 } }' enter image description here I get HTML instead of JSON.

Calendar event is getting created however. But I need the event ID as a result. Can anyone share an idea, please?

Googling did not give me any results yet, but I'm not stopping.

CodePudding user response:

From your following tested curl command,

curl --location --request POST 'https://script.google.com/macros/s/AKfycbyh7n3YeE-HiNAIA8wi9HAVsaLBUv5ceJu-k7yxL4D8mSm9EXQ4wQc_ctqipFlAR4SqfA/exec'
--header 'Content-Type: application/json'
--data-raw '{ "token": "l056SH7REYs************3IYXeiECDIX0e08XvsBAtzA2eSJg", "calendarId": "[email protected]", "event": { "summary":"TEst from postman", "description":"Abyrvalg", "start": { "date": "2022-11-28" }, "end": { "date": "2022-11-28" }, "colorID": 9 } }'

In this case, how about the following modification?

Modified curl command:

curl --location 'https://script.google.com/macros/s/AKfycbyh7n3YeE-HiNAIA8wi9HAVsaLBUv5ceJu-k7yxL4D8mSm9EXQ4wQc_ctqipFlAR4SqfA/exec' \
--data-raw '{ "token": "l056SH7REYs************3IYXeiECDIX0e08XvsBAtzA2eSJg", "calendarId": "[email protected]", "event": { "summary":"TEst from postman", "description":"Abyrvalg", "start": { "date": "2022-11-28" }, "end": { "date": "2022-11-28" }, "colorID": 9 } }'

Reference:

  • Related