假設在Marketing Cloud有這樣一個contact主數據:node
如今需求是使用編程語言好比nodejs修改這個contact實例的高亮屬性。編程
代碼以下:json
var config = require("./mcConfig");
var request = require('request');
var url = config.tokenURL;
console.log("user: " + config.user + " password: " + config.password);
var getTokenOptions = {
url: url,
method: "GET",
json:true,
headers: {
'Authorization': 'Basic ' + new Buffer(config.user + ":" + config.password).toString('base64'),
"content-type": "application/json",
"x-csrf-token" :"fetch"
}
};
function getToken() {
return new Promise(function(resolve,reject){
var requestC = request.defaults({jar: true});
console.log("Step1: get csrf token via url: " + url );
requestC(getTokenOptions,function(error,response,body){
var csrfToken = response.headers['x-csrf-token'];
if(!csrfToken){
reject({message:"token fetch error: " + error});
return;
}
console.log("Step1: csrf token got: " + csrfToken);
resolve(csrfToken);
});
});
}
function updateContact(token){
return new Promise(function(resolve, reject){
var sPostData = "--batch_1f7d-bd35-caed" + "\n" +
"Content-Type: multipart/mixed; boundary=changeset_8f9e-9a44-9f9e" + "\n" +
"\n" +
"--changeset_8f9e-9a44-9f9e" + "\n" +
"Content-Type: application/http" + "\n" +
"Content-Transfer-Encoding: binary" + "\n" +
"\n" +
"MERGE Consumers('02000A21209F1EE99CDF1A1FC9AA8065')?sap-client=100 HTTP/1.1" + "\n" +
"Cache-Control: max-age=360" + "\n" +
"sap-contextid-accept: header" + "\n" +
"Accept: application/json" + "\n" +
"Accept-Language: en" + "\n" +
"DataServiceVersion: 2.0" + "\n" +
"MaxDataServiceVersion: 2.0" + "\n" +
"x-csrf-token: fQ2Pwfmf0K_LVYoKV9QYUw==" + "\n" +
"Content-Type: application/json" + "\n" +
//"Content-Length: 215" + "\n" +
"\n" +
"{\"YY1_CustomerType_ENH\":\"Jerry測試1\"}" + "\n" +
"--changeset_8f9e-9a44-9f9e--" + "\n" +
"\n" +
"--batch_1f7d-bd35-caed--";
var requestC = request.defaults({jar: true});
var createOptions = {
url: config.updateContactURL,
method: "POST",
json:false,
headers: {
"content-type": "multipart/mixed;boundary=batch_1f7d-bd35-caed",
'x-csrf-token': token
},
body:sPostData
};
requestC(createOptions,function(error,response,data){
if(error){
reject(error.message);
}else {
debugger;
console.log("Contact updated successfully");
resolve(data);
}
});
});
}
getToken().then(updateContact).catch((error) =>{
console.log("error: " + error.message);
});
複製代碼
我在nodejs代碼裏把須要更改的字段值賦爲"Jerry測試1」:app
執行以後這個屬性被成功更新了:編程語言
要獲取更多Jerry的原創文章,請關注公衆號"汪子熙": 測試