[英]How to send a POST request from App Script to Firebase with an array
I'm trying to send a POST request from an App Script application to a firebase cloud function.我正在尝试将 POST 请求从 App Script 应用程序发送到 firebase 云 function。 But I'm not being able to send correctly arrays inside this request.
但我无法在此请求中正确发送 arrays 。 The request is sending the array in a messy way that the cloud function can't read it correctly
请求以凌乱的方式发送数组,导致云 function 无法正确读取
I'm using the following code:我正在使用以下代码:
const data = {
var1: 'string',
var2: {name: 'object'},
var3: new Date(),
var4: [1, 2, 3],
var5: [{name: 'any object'}, {name: 'inside an array'}],
}
const url = "https://us-central1-my-app.cloudfunctions.net/executeFunction";
const options = {
method: "post",
"Content-Type": "application/json", //I tried to change this content-type for other options
payload: data
};
const request = UrlFetchApp.fetch(url, options);
On the server side I tried to read the data using the following ways:在服务器端,我尝试使用以下方式读取数据:
export const executeFunction = functions.https.onRequest(async(req, res) => {
//option 1
const data = JSON.parse(req.body);
res.send(JSON.stringify(data));
//option2
const data = req.body;
res.send(JSON.stringify(data);
})
But on the server the req.body
returns me the following value:但在服务器上
req.body
返回我以下值:
{
"var1":"string",
"var2":{"name":"object"},
"var3":"2020-07-14T18:45:21.946Z",
"var4":[Ljava.lang.Object;@5e1b03dd, // :(
"var5":[Ljava.lang.Object;@19d93f99 // :(
How can I get the array values correctly?如何正确获取数组值?
I suspect that it may be something related to the charset, but I already tried using "Content-Type": "application/json; charset=utf-8"
on the options
object and it didn't work.我怀疑它可能与字符集有关,但我已经尝试在
options
object 上使用"Content-Type": "application/json; charset=utf-8"
并且它不起作用。
Any ideas on how to solve it?关于如何解决它的任何想法? :)
:)
Update:更新:
Thanks @Pickachu谢谢@皮卡丘
I had to change 2 things: -stringify the payload -change the key on options from "Content-Type" to "contentType"我必须更改 2 件事: - 对有效负载进行字符串化 - 将选项的键从“Content-Type”更改为“contentType”
Here the code:这里的代码:
const options = {
method: "post",
contentType: "application/json", //changed here
payload: JSON.stringify(data) //and here
};
const request = UrlFetchApp.fetch(url, options);
You are almost getting it.你几乎明白了。 A POST request from UrlFetcgApp with a json payload should have the payload serialized:
来自 UrlFetcgApp 的带有 json 有效负载的 POST 请求应将有效负载序列化:
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object) https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)
const options = {
...
payload: JSON.stringify(data)
};
And a firebase cloud function using a https trigger automatically parses the json body so there is no need to parse or stringify it. And a firebase cloud function using a https trigger automatically parses the json body so there is no need to parse or stringify it.
export const executeFunction = functions.https.onRequest(async(req, res) => {
const data = req.body;
console.log(data.var1);
console.log(data.var2);
console.log(data.var3);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.