简体   繁体   English

HTTP POST 到 Google 表格(通过 Apps 脚本)未导入 JSON 正文消息内容?

[英]HTTP POST to Google Sheets (via Apps Script) not importing JSON body message contents?

I've designed a web app that posts order information to Google Sheets via HTTP POST and Google Apps Script.我设计了一个 web 应用程序,它通过 HTTP POST 和 Google Apps 脚本将订单信息发布到 Google 表格。 However, I am struggling to POST and extract the order information from the body JSON.但是,我正在努力发布并从正文 JSON 中提取订单信息。 I have tried 2 different methods and they both output the similar information but with the same data type - FileUpload.我尝试了 2 种不同的方法,它们都 output 具有相似的信息但具有相同的数据类型 - FileUpload。 I've even attempted to import it as parameters as FormData() but repeatedly appending parameters didn't feel quite right.我什至尝试将其作为 FormData() 的参数导入,但反复附加参数感觉不太正确。

Method 1方法一

function method1() {
  var xml = new XMLHttpRequest();
  var data = JSON.stringify({ firstName: "Jay", lastName: "Smith" });
  xml.open("POST", url, true);
  /*   xml.setRequestHeader("Content-type", "application/json"); */
  xml.onreadystatechange = function () {
    if (xml.readyState === 4 && xml.status === 200) {
      alert(xml.responseText);
    }
  };
  xml.send(data);
}

Output Output

{postData=FileUpload, contextPath=, contentLength=38.0, parameters={}, parameter={}, queryString=}

Notice how the setRequestHeader() function is commented.注意 setRequestHeader() function 是如何被注释的。 No output would occur if it was uncommented making the content type set to JSON.如果取消注释将内容类型设置为 JSON,则不会出现 output。 Why will it not work?为什么它不起作用?

Method 2方法二

function method2() {
  const body = {
    firstName: "Jay",
    lastName: "Smith",
  };
  const options = {
    method: "POST",
    body: JSON.stringify(body),
  };

  fetch(url, options).then((res) => res.json());
}

Output Output

{contextPath=, parameters={}, parameter={}, queryString=, postData=FileUpload, contentLength=38.0}

Method 3 (Should I even consider this?)方法3(我什至应该考虑这个吗?)

function method3() {
  const formData = new FormData();
  formData.append("firstName", "Jay");
  formData.append("lastName", "Smith");
  fetch(url, {
    method: "POST",
    body: formData,
  }).then((res) => res.json());
}

Output Output

{contentLength=243.0, parameter={firstName=Jay, lastName=Smith}, queryString=, contextPath=, parameters={lastName=[Ljava.lang.Object;@6c4aece6, firstName=[Ljava.lang.Object;@3c461e46}}

The content length is considerably larger... this does not seem like an effective concept.内容长度要大得多……这似乎不是一个有效的概念。


Google Apps Script doPost() Function Google Apps 脚本 doPost() Function

function doPost(e) {
  const sheet = SpreadsheetApp.openById(SHEETID).getSheetByName('Testing Inputs');
  sheet.getRange(3,1).setValue(e); // To display POST output
  const data = JSON.parse(request.postData.contents)
  sheet.getRange(5,1).setValue(data['firstName']) // Not writing "Jay"? 
  sheet.getRange(5,2).setValue(data['lastName']) // Not writing "Smith"?
}

In general, it appears data is coming through as body message content judging by the contentLength output but is it not JSON considering the postData is FileUpload?一般来说,根据 contentLength output 判断,似乎数据是作为正文消息内容通过的,但考虑到 postData 是 FileUpload,它不是 JSON 吗? That may cause my Apps Script nor to be able to extract it right?这可能会导致我的 Apps 脚本也无法提取它吗? Also, I thought I read that using fetch() is considerably better than using XMLHttpRequest() so should I be using my Method 2?另外,我认为我读到使用 fetch() 比使用 XMLHttpRequest() 好得多,所以我应该使用我的方法 2 吗? Still not sure what am I doing wrong to populate it in individual cells in sheets using Apps Script?仍然不确定使用 Apps 脚本将其填充到工作表中的单个单元格中我做错了什么?

Any help is appreciated!任何帮助表示赞赏!

Output Using sheet.getRange(3,1).setValue(JSON.stringify(e)) Output 使用sheet.getRange(3,1).setValue(JSON.stringify(e))

{"parameter":{},"postData":{"contents":"{\"firstName\":\"Jay\",\"lastName\":\"Smith\"}","length":38,"name":"postData","type":"text/plain"},"parameters":{},"contentLength":38,"queryString":"","contextPath":""}

Based on the output provided by sheet.getRange(3,1).setValue(JSON.stringify(e)) (I'm not sure which of your methods generated this, but that method looks like it's the right one), your doPost as written should work, with one modification: I don't see a variable request defined, so I assume you intend request to be the http request object, which in an Apps Script web app is defined by the e parameter passed to the doPost ( documentation ).基于sheet.getRange(3,1).setValue(JSON.stringify(e))提供的 output (我不确定你的哪个方法生成了这个,但那个方法看起来是正确的),你的doPost所写的应该可以工作,有一个修改:我没有看到定义的变量request ,所以我假设您打算request成为 http 请求doPost ,在应用程序脚本e中定义文档)。 Therefore change this line因此更改此行

const data = JSON.parse(request.postData.contents)

to this one对这个

const data = JSON.parse(e.postData.contents)

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

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