使用nodejs实现OData的batch操作在Marketing Cloud里读取contact信息

简介: 我们先来看看Marketing Cloud系统里的contact信息:一共1218374条数据。我们用如下的nodejs代码通过OData来获取这些数据:var request = require('request');var config = require(".

我们先来看看Marketing Cloud系统里的contact信息:
一共1218374条数据。

我们用如下的nodejs代码通过OData来获取这些数据:

var request = require('request');
var config = require("./mcConfig");

var url = config.getContactBatchURL;

var sBody = "--batch_c914-a60c-1877" + "\n" + 
"Content-Type: application/http" + "\n" + 
"Content-Transfer-Encoding: binary" + "\n" + 
"\n" + 
"GET InteractionContacts?sap-client=100&$skip=0&$top=2&$select=ImageURL%2cName%2cContactLevelName%2cCountryName%2cCity%2cEMailAddress%2cPhoneNumber%2cMobilePhoneNumber%2cCorporateAccountName%2cInteractionContactUUID%2cRelationship%2cType&$inlinecount=allpages HTTP/1.1" + 
"sap-cancel-on-close: true" + "\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" + 
"\n" + "\n" + 
"--batch_c914-a60c-1877--";

var getContactOptions = {
        url: url,
        method: "POST",
        json:false,
        headers: {
            "content-type": "multipart/mixed;boundary=batch_c914-a60c-1877",
            'Authorization': 'Basic ' + new Buffer(config.user + ":" + config.password).toString('base64')
        },
        body: sBody
};

function getContact() {
  return new Promise(function(resolve,reject){
      var requestC = request.defaults({jar: true});
      console.log("Step1: get contact via url: " + url );

      requestC(getContactOptions,function(error,response,body){
        if( error){
          console.log("error occurred: " + error);
          reject(error);
        }
        console.log("response:" + body);
        var nStartIndex = body.indexOf("{");
        var nLastIndex = body.lastIndexOf("}");
        if( nStartIndex < 0 || nLastIndex < 0)
            return;
        var sPayload = body.substring(nStartIndex, ++nLastIndex);
        resolve(JSON.parse(sPayload));
      }); 
     });
}

function displayResult(oResult){
  console.log(oResult);
}

getContact().then(displayResult);

使用node命令直接执行这个.js文件:

得到结果:

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关文章
|
JavaScript 前端开发 Linux
Node.js 获取文件信息及路径
Node.js 获取文件信息及路径
|
5月前
|
数据库 存储 监控
什么是 SAP HANA 内存数据库 的 Delta Storage
什么是 SAP HANA 内存数据库 的 Delta Storage
什么是 SAP HANA 内存数据库 的 Delta Storage
|
11月前
|
Windows
ts-node : 无法加载文件 C:\Users\Dell\AppData\Roaming\npm\ts-node.ps1,因为在此系统上禁止运行脚本。有关详细信息
ts-node : 无法加载文件 C:\Users\Dell\AppData\Roaming\npm\ts-node.ps1,因为在此系统上禁止运行脚本。有关详细信息
245 0
|
1月前
crash —— 获取系统NODE、ZONE、SECTION、MEMBLOCK等信息
crash —— 获取系统NODE、ZONE、SECTION、MEMBLOCK等信息
|
2月前
|
搜索推荐 Java API
Electron V8排查问题之分析 node-memwatch 提供的堆内存差异信息来定位内存泄漏对象如何解决
Electron V8排查问题之分析 node-memwatch 提供的堆内存差异信息来定位内存泄漏对象如何解决
55 0
|
2月前
|
JavaScript Windows
NodeJs——如何获取Windows电脑指定应用进程信息
NodeJs——如何获取Windows电脑指定应用进程信息
75 0
|
5月前
|
数据采集 JavaScript 数据可视化
Node.js爬虫在租房信息监测与分析中的应用
Node.js爬虫在租房信息监测与分析中的应用
|
JavaScript 网络协议 测试技术
关于 SAP Commerce Cloud 构建的 Node.js 版本号问题
关于 SAP Commerce Cloud 构建的 Node.js 版本号问题
|
JavaScript 前端开发 安全
windows 环境下使用 Node.js 访问 SAP OData 遇到 unable to get local issuer certificate
windows 环境下使用 Node.js 访问 SAP OData 遇到 unable to get local issuer certificate
npm : 无法加载文件 D:\soft\codeProcess\node\node_global\npm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.micr +
npm : 无法加载文件 D:\soft\codeProcess\node\node_global\npm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.micr +
npm : 无法加载文件 D:\soft\codeProcess\node\node_global\npm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.micr +