如何获取Google Apps脚本doPost()来读取我的POST请求? [英] How to get Google Apps Script doPost() to read my POST request?

查看:198
本文介绍了如何获取Google Apps脚本doPost()来读取我的POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js和Puppeteer从无头浏览器运行一个抓取项目.我想将数据发布到Google Apps脚本进行进一步处理.我希望在GAS项目中看到带有填充参数的数据.但是,我得到的结果只有空参数.

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.

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

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

这是生成该响应的GAS代码.

Here is the GAS code that generates that response.

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.

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

我已使用[RequestBin] [1]验证我正在发送有效的POST请求.

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

我在做什么错了?

推荐答案

请查看您的状态

Please review how you're publishing the web app.

  • 以以下身份执行该应用程序:我
  • 有权访问该应用的人:所有人,甚至是匿名人

您的POST&的URL GET应该类似于https://script.google.com/macros/s/IDENTIFIER/exec,而不是https://script.googleusercontent.com/macros/echo?user_content_key=[key].后一个URL看起来像是从发布任何人,甚至匿名"都无法访问的Web应用程序的重定向.

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

返回

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

这篇关于如何获取Google Apps脚本doPost()来读取我的POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆