google-apps-scriptweb-applicationsgoogle-workspacehttp-status-code-400

400 error on calling Google Apps Script from Wordpress


I created and exposed this very simple Google Apps Script:

function doPost(e) {
  try {
    Logger.log('Data received : ' + JSON.stringify(e));
        return ContentService.createTextOutput(JSON.stringify({status: 'success'})).setMimeType(ContentService.MimeType.JSON);
  } catch (error) {
    Logger.log('Error: ' + error.message);
    return ContentService.createTextOutput(JSON.stringify({status: 'error', message: error.message})).setMimeType(ContentService.MimeType.JSON);
  }
}

function doGet(e) {
  return ContentService.createTextOutput("Only POST request accepted.");

With these settings: Execute as me and everyone can access.

But when I try to call it from wordpress it fails with 400 error.

I can see the call in the executions historic from GAS editor but with no logs at all.

Here is the result of this curl command:

curl -L -X POST https://script.google.com/macros/s/AKfycbxxb17haq53hMcRnfPIwIz9vNamPar4qXYKEmToq2gOTpw9iHRIrc7DnTXgg7c45PS3/exec \
-H "Content-Type: application/json" \
-d '{"email": "test@example.com", "progression": 50, "id_formation": "formation123"}'
Page introuvable.goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child+html .goog-inline-block{display:inline}#drive-logo{margin:18px 0;position:absolute;white-space:nowrap}.docs-drivelogo-img{background-image:url(//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png);-webkit-background-size:116px 41px;background-size:116px 41px;display:inline-block;height:41px;vertical-align:bottom;width:116px}.docs-drivelogo-text{color:#000;display:inline-block;opacity:.54;text-decoration:none;font-family:"Product Sans",Arial,Helvetica,sans-serif;font-size:32px;text-rendering:optimizeLegibility;position:relative;top:-6px;left:-7px;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:144dpi){.docs-drivelogo-img{background-image:url(//ssl.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_116x41dp.png)}}sentinel{}body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, a:link, a:visited {color: #112ABB;}.errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;} DriveImpossible d'ouvrir le fichier pour le moment.

Vérifiez l'adresse, puis réessayez.

Efficacité maximale avec Google Drive

Les applications Google Drive simplifient la création, le stockage et le partage en ligne de documents, feuilles de calcul, présentations et bien plus encore.

Pour plus d'informations, consultez la page drive.google.com/start/apps.

html {height: 100%; overflow: auto;}body {height: 100%; overflow: auto;}#outerContainer {margin: auto; max-width: 750px;}#innerContainer {margin-bottom: 20px; margin-left: 40px; margin-right: 40px; margin-top: 80px; position: relative;}%

In the meantime I made more investigation.

I found a way to write logs in a Google Sheet file and here is what I found :

Here is the data sent by postman with success:

{
    "contentLength": 0,
    "parameters":
    {
        "email":
        [
            "john_doe@fakemail.com"
        ],
        "progression":
        [
            "50"
        ],
        "id_formation":
        [
            "5"
        ]
    },
    "contextPath": "",
    "parameter":
    {
        "email": "john_doe@fakemail.com",
        "progression": "50",
        "id_formation": "5"
    },
    "queryString": "email=john_doe@fakemail.com&progression=50&id_formation=5"
}

And here is the data sent by Thrive Auto with 400 error and something that looks odd to me but I'm not an expert (see this part : {"postData":{"contents":"{"":""}","length":7,"name":"postData","type":"application/json"}):

{
    "postData":
    {
        "contents": "{\"\":\"\"}",
        "length": 7,
        "name": "postData",
        "type": "application/json"
    },
    "contextPath": "",
    "parameters":
    {
        "progression":
        [
            "50"
        ],
        "email":
        [
            "john_doe@fakemail.com"
        ],
        "id_formation":
        [
            "5"
        ]
    },
    "parameter":
    {
        "email": "john_doe@fakemail.com",
        "id_formation": "5",
        "progression": "50"
    },
    "contentLength": 7,
    "queryString": "email=john_doe@fakemail.com&progression=50&id_formation=5"
}

Regards, Florent


Solution

  • In your showing curl command and With these settings: Execute as me and everyone can access., please modify as follows and test it again.

    Modified curl command:

    curl -L https://script.google.com/macros/s/AKfycbxxb17haq53hMcRnfPIwIz9vNamPar4qXYKEmToq2gOTpw9iHRIrc7DnTXgg7c45PS3/exec \
    -d '{"email": "test@example.com", "progression": 50, "id_formation": "formation123"}'
    

    About I can see the call in the executions historic from GAS editor but with no logs at all., in this case, this information might be useful. Ref (Author: me)

    Note:

    References: