简体   繁体   English

如何让 Google Apps 脚本 doPost() 读取我的 POST 请求?

[英]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.我正在使用 node.js 和 Puppeteer 从无头浏览器运行抓取项目。 I want to post the data to a Google Apps Script for further processing.我想将数据发布到 Google Apps 脚本以进行进一步处理。 I expect to see the data in my GAS project with populated parameters.我希望在我的 GAS 项目中看到填充参数的数据。 But instead, I get the following result with only empty parameters.但相反,我得到以下结果,只有空参数。

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

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

Here is the GAS code that generates that response.这是生成该响应的 GAS 代码。

Code.gs 代码.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 刮刀.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.我已使用 [RequestBin][1] 验证我正在发送有效的 POST 请求。

What am I doing wrong?我究竟做错了什么?

Please review how you're publishing the web app.请查看您发布web 应用程序的方式。

  • 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 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".后者 URL 看起来像是发布 web 应用程序的重定向,“任何人,甚至匿名”都无法访问。

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"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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