简体   繁体   中英

How to get Google Apps Script doPost() to read my POST request?

I am running a scraping project from a headless browser using node.js and Puppeteer. I want to post the data to a Google Apps Script for further processing. I expect to see the data in my GAS project with populated parameters. But instead, I get the following result with only empty parameters.

https://script.googleusercontent.com/macros/echo?user_content_key=[key]

{"parameter":{},"contextPath":"","contentLength":-1,"queryString":"","parameters":{}}

Here is the GAS code that generates that response.

Code.gs
 function doGet(e){ return handleResponse(e); } function doPost(e){ return handleResponse(e); } function handleResponse(e) { var json = JSON.stringify(e) var textOutput = ContentService.createTextOutput(json); return textOutput }

Here is the code I am using to send the request.

scraper.js
 const request = require('request'); request({ method: POST, preambleCRLF: true, postambleCRLF: true, uri: postUrl, multipart: { chunked: false, data, }, },

I have verified using [RequestBin][1] that I am sending a valid POST request.

What am I doing wrong?

Please review how you're publishing the web app.

  • Execute the app as: Me
  • Who has access to the app: Anyone, even anonymous

The URL for your POST & GET should be something like https://script.google.com/macros/s/IDENTIFIER/exec , not https://script.googleusercontent.com/macros/echo?user_content_key=[key] . The latter URL looks like a redirect from publishing a web app that is not accessible to "Anyone, even anonymous".

If that's set correctly, this request

curl --request POST \
  --url 'https://script.google.com/macros/s/IDENTIFIER/exec?param1=1&param2=2' \
  --header 'content-type: application/json' \
  --data '{"json": true}'

returns

{
  "parameter": {
    "param1": "1",
    "param2": "2"
  },
  "contextPath": "",
  "contentLength": 14,
  "queryString": "param1=1&param2=2",
  "parameters": {
    "param1": [
      "1"
    ],
    "param2": [
      "2"
    ]
  },
  "postData": {
    "type": "application/json",
    "length": 14,
    "contents": "{\"json\": true}",
    "name": "postData"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM