如何通过POST请求以JSON格式在Google表格脚本中获取数据? [英] How to take data in google sheet script via POST request in JSON format?

查看:171
本文介绍了如何通过POST请求以JSON格式在Google表格脚本中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于从某个地方接收POST请求的.我正在寻找一个Google工作表脚本函数,该函数可以从JSON格式的POST请求中获取和处理数据.你能举个例子吗?

This question is about receiving POST request from somewhere. I'm looking for a google sheet script function that can take and handle data from the POST request in JSON format. Could you suggest any example?

POST请求在这里:

The POST request is here:

https://script.google.com/macros/s/BOdirjv45Dn6FHrx_4GUguuS6NJxnSEeviMHm3HerJl4UsDBnDgfFPO/

{
  "p1": "writeTitle",
  "p2": [[URL]],
  "p3": [[PIC_A]],
  "p4": [[PIC_B]],
  "p5": [[TITLE]]
}

application/json

doPost()不起作用:

doPost(e) {
var json = JSON.parse(e.postData.contents);
Logger.log(json);
}

推荐答案

  • 您要从请求正文中检索作为对象的值.
  • 您已经部署了Web Apps.
  • 如果我对您的情况的理解是正确的,那么如何进行修改?

    If my understanding of your situation is correct, how about this modification?

    作为示例,我使用以下curl命令将POST张贴到Web Apps.

    As a sample, I used the following curl command to POST to Web Apps.

    curl -L \
    -H 'Content-Type:application/json' \
    -d '{"p1": "writeTitle","p2": "[[URL]]","p3": "[[PIC_A]]","p4": "[[PIC_B]]","p5": "[[TITLE]]"}' \
    "https://script.google.com/macros/s/#####/exec"
    

    运行上述命令时,doPost(e)中的e如下.

    When above command is run, e of doPost(e) is as follows.

    {
      "parameter": {},
      "contextPath": "",
      "contentLength": 90,
      "queryString": "",
      "parameters": {},
      "postData": {
        "type": "application/json",
        "length": 90,
        "contents": "{\"p1\": \"writeTitle\",\"p2\": \"[[URL]]\",\"p3\": \"[[PIC_A]]\",\"p4\": \"[[PIC_B]]\",\"p5\": \"[[TITLE]]\"}",
        "name": "postData"
      }
    }
    

    已发布的有效负载可以由e.postData检索.从上面的响应中发现,您可以通过e.postData.contents检索所需的值.顺便说一句,当查询参数和有效载荷如下给出时,

    The posted payload can be retrieved by e.postData. From above response, it is found that the value you want can be retrieved by e.postData.contents. By the way, when the query parameter and the payload are given like as follows,

    curl -L \
    -H 'Content-Type:application/json' \
    -d '{"p1": "writeTitle","p2": "[[URL]]","p3": "[[PIC_A]]","p4": "[[PIC_B]]","p5": "[[TITLE]]"}' \
    "https://script.google.com/macros/s/#####/exec?key=value"
    

    value可以通过e.parametere.parameters检索.有效载荷可以通过e.postData.contents检索.

    value can be retrieved by e.parameter or e.parameters. And the payload can be retrieved by e.postData.contents.

    在此修改后的脚本中,可以在Stackdriver上看到结果,并返回结果.

    In this modified script, the result can be seen at the Stackdriver, and also the result is returned.

    function doPost(e) {
      var json = JSON.parse(e.postData.contents);
      console.log(json);
      return ContentService.createTextOutput(JSON.stringify(json));
    }
    

    注意:

    • 在修改Web Apps脚本时,请重新部署它为新版本.这样,最新脚本将反映到Web Apps.这是重要的一点.
      • Web Apps
      • Stackdriver Logging

      如果这不是您想要的,对不起.

      If this was not what you want, I'm sorry.

      这篇关于如何通过POST请求以JSON格式在Google表格脚本中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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