除非我提供陣列鍵,否則我無法發布一組值。(請參閱我的代碼中的輸出)
有人可以告訴使用 發布陣列的正確方法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"
// }
}
uj5u.com熱心網友回復:
在您的情況下,根據您提供的檔案,我提出了以下修改。
從您提供的檔案中,我找到了以下示例 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 Script 實作這一點,下面的修改如何?
修改后的腳本:
在此修改中,為了將 的物件轉換為{'tags[]': [1, 2]}查詢引數,我使用了這個示例腳本。
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]});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/449470.html
標籤:谷歌应用脚本
