如何使用 `UrlFetchApp.fetch` 和 `application/x-www-form-urlencoded` post 值数组?
How to post an array of values using `UrlFetchApp.fetch` with `application/x-www-form-urlencoded`?
我无法 post 值数组,除非我提供数组键。 (查看我的代码中的输出)
有人可以告诉 post 使用 application/x-www-form-urlencoded
的数组的正确方法吗?
function test() {
function post(payload) {
const url = 'https://my.php.post.server/';
// Content of PHP: var_dump($_POST['tags']);
const options = {
'method': 'POST',
'payload': payload
}
const response = UrlFetchApp.fetch(url, options);
const content = response.getContentText();
console.log(content);
}
post({
'tags': [1, 2]
});
// string(28) "[Ljava.lang.Object;@71c0475b"
post({
'tags[]': [1, 2]
});
// array(1) {
// [0]=>
// string(27) "[Ljava.lang.Object;@9bc5c1d"
// }
post({
'tags': 1,
'tags': 2
});
// string(3) "2.0"
post({
'tags[]': 1,
'tags[]': 2
});
// array(1) {
// [0]=>
// string(3) "2.0"
// }
post({
'tags[0]': 1,
'tags[1]': 2
});
// array(2) {
// [1]=>
// string(3) "2.0"
// [0]=>
// string(3) "1.0"
// }
}
针对你的情况,根据your provided document,我提出如下修改。
从您提供的文档中,我找到了以下示例 curl 命令。
curl https://pi.pardot.com/api/email/version/4/do/send \
--header "Authorization: Bearer <ACCESS TOKEN>" \
--header'Pardot-Business-Unit-Id: <BUSINESS UNIT ID>'
--data-urlencode campaign_id=12345 \
--data-urlencode from_assigned_user=1 \
--data-urlencode email_template_id=6789 \
--data-urlencode list_ids[]=123 \
--data-urlencode list_ids[]=456 \
--data-urlencode list_ids[]=789 \
--data-urlencode list_ids[]=101 \
--data-urlencode suppression_list_ids[]=987 \
--data-urlencode suppression_list_ids[]=654 \
--data-urlencode tags[]=new_prospect_email \
--data-urlencode tags[]=cart_abandoned \
--data-urlencode tags[]=subtotal_over_200 \
--data-urlencode scheduled_time=2021-10-31T17:00:00-0400 \
--data-urlencode format=json
在这种情况下,我认为当你想使用{'tags[]': [1, 2]}
时,需要按application/x-www-form-urlencoded
的内容类型请求作为表单数据。为了使用 Google Apps 脚本实现此目的,如何进行以下修改?
修改后的脚本:
本次修改,为了将{'tags[]': [1, 2]}
的对象转换为查询参数,我使用了this sample script.
function test() {
// This is from https://tanaikech.github.io/2018/07/12/adding-query-parameters-to-url-using-google-apps-script/
String.prototype.addQuery = function(obj) {
return this + Object.keys(obj).reduce(function(p, e, i) {
return p + (i == 0 ? "?" : "&") +
(Array.isArray(obj[e]) ? obj[e].reduce(function(str, f, j) {
return str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : "")
},"") : e + "=" + encodeURIComponent(obj[e]));
},"");
}
function post(payload) {
const url = 'https://my.php.post.server/';
// Content of PHP: var_dump($_POST['tags']);
const options = {
'method': 'POST',
'payload': "".addQuery(payload).replace("?", ""),
}
const response = UrlFetchApp.fetch(url, options);
const content = response.getContentText();
console.log(content);
}
post({"tags[]": [1, 2]});
}
- 通过此修改,
{"tags[]": [1, 2]}
作为表单数据发送。
注:
这是一个简单的示例脚本,用于解释如何实现您的目标。请根据您的情况进行修改。
当addQuery
根据你的情况修改后,变成如下
const convertQuery = obj => Object.keys(obj).reduce((p, e) =>
p + (Array.isArray(obj[e]) ? obj[e].reduce((str, f, j) =>
str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : ""),"") : e + "=" + encodeURIComponent(obj[e])) ,"");
function post(payload) {
const url = 'https://my.php.post.server/';
// Content of PHP: var_dump($_POST['tags']);
const options = {
'method': 'POST',
'payload': convertQuery(payload)
}
const response = UrlFetchApp.fetch(url, options);
const content = response.getContentText();
console.log(content);
}
post({"tags[]": [1, 2]});
我无法 post 值数组,除非我提供数组键。 (查看我的代码中的输出)
有人可以告诉 post 使用 application/x-www-form-urlencoded
的数组的正确方法吗?
function test() {
function post(payload) {
const url = 'https://my.php.post.server/';
// Content of PHP: var_dump($_POST['tags']);
const options = {
'method': 'POST',
'payload': payload
}
const response = UrlFetchApp.fetch(url, options);
const content = response.getContentText();
console.log(content);
}
post({
'tags': [1, 2]
});
// string(28) "[Ljava.lang.Object;@71c0475b"
post({
'tags[]': [1, 2]
});
// array(1) {
// [0]=>
// string(27) "[Ljava.lang.Object;@9bc5c1d"
// }
post({
'tags': 1,
'tags': 2
});
// string(3) "2.0"
post({
'tags[]': 1,
'tags[]': 2
});
// array(1) {
// [0]=>
// string(3) "2.0"
// }
post({
'tags[0]': 1,
'tags[1]': 2
});
// array(2) {
// [1]=>
// string(3) "2.0"
// [0]=>
// string(3) "1.0"
// }
}
针对你的情况,根据your provided document,我提出如下修改。
从您提供的文档中,我找到了以下示例 curl 命令。
curl https://pi.pardot.com/api/email/version/4/do/send \
--header "Authorization: Bearer <ACCESS TOKEN>" \
--header'Pardot-Business-Unit-Id: <BUSINESS UNIT ID>'
--data-urlencode campaign_id=12345 \
--data-urlencode from_assigned_user=1 \
--data-urlencode email_template_id=6789 \
--data-urlencode list_ids[]=123 \
--data-urlencode list_ids[]=456 \
--data-urlencode list_ids[]=789 \
--data-urlencode list_ids[]=101 \
--data-urlencode suppression_list_ids[]=987 \
--data-urlencode suppression_list_ids[]=654 \
--data-urlencode tags[]=new_prospect_email \
--data-urlencode tags[]=cart_abandoned \
--data-urlencode tags[]=subtotal_over_200 \
--data-urlencode scheduled_time=2021-10-31T17:00:00-0400 \
--data-urlencode format=json
在这种情况下,我认为当你想使用{'tags[]': [1, 2]}
时,需要按application/x-www-form-urlencoded
的内容类型请求作为表单数据。为了使用 Google Apps 脚本实现此目的,如何进行以下修改?
修改后的脚本:
本次修改,为了将{'tags[]': [1, 2]}
的对象转换为查询参数,我使用了this sample script.
function test() {
// This is from https://tanaikech.github.io/2018/07/12/adding-query-parameters-to-url-using-google-apps-script/
String.prototype.addQuery = function(obj) {
return this + Object.keys(obj).reduce(function(p, e, i) {
return p + (i == 0 ? "?" : "&") +
(Array.isArray(obj[e]) ? obj[e].reduce(function(str, f, j) {
return str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : "")
},"") : e + "=" + encodeURIComponent(obj[e]));
},"");
}
function post(payload) {
const url = 'https://my.php.post.server/';
// Content of PHP: var_dump($_POST['tags']);
const options = {
'method': 'POST',
'payload': "".addQuery(payload).replace("?", ""),
}
const response = UrlFetchApp.fetch(url, options);
const content = response.getContentText();
console.log(content);
}
post({"tags[]": [1, 2]});
}
- 通过此修改,
{"tags[]": [1, 2]}
作为表单数据发送。
注:
这是一个简单的示例脚本,用于解释如何实现您的目标。请根据您的情况进行修改。
当
addQuery
根据你的情况修改后,变成如下const convertQuery = obj => Object.keys(obj).reduce((p, e) => p + (Array.isArray(obj[e]) ? obj[e].reduce((str, f, j) => str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : ""),"") : e + "=" + encodeURIComponent(obj[e])) ,""); function post(payload) { const url = 'https://my.php.post.server/'; // Content of PHP: var_dump($_POST['tags']); const options = { 'method': 'POST', 'payload': convertQuery(payload) } const response = UrlFetchApp.fetch(url, options); const content = response.getContentText(); console.log(content); } post({"tags[]": [1, 2]});