Skip to content

Instantly share code, notes, and snippets.

@danieljpeter
Created July 23, 2017 17:41
Show Gist options
  • Save danieljpeter/b2348299488f8fee8631532f350fb21b to your computer and use it in GitHub Desktop.
Save danieljpeter/b2348299488f8fee8631532f350fb21b to your computer and use it in GitHub Desktop.
Example code of how to upload an excel / pbix file to Power BI embedded
/*
Example code of how to upload an excel / pbix file to Power BI embedded
uses this API here: https://msdn.microsoft.com/en-us/library/mt243840.aspx
*/
var group_id = '<app workspace id>';
var access_token = '<power bi access token>';
var file = fs.readFileSync('pbix.pbix');
var boundaryKey = Math.random().toString(16); //random string
var start =
'--' + boundaryKey + '\r\n' +
'Content-Disposition: form-data; name="theFile"; filename="pbix.pbix"\r\n' +
'Content-Type: application/octet-stream\r\n\r\n';
var end = '\r\n--' + boundaryKey + '--\r\n';
var bBody = Buffer.concat([Buffer.from(start), file, Buffer.from(end)]);
console.log('bBody.byteLength: ' + bBody.byteLength);
var importPath = '/v1.0/myorg/groups/'+group_id+'/imports?datasetDisplayName=mydataset01&nameConflict=Ignore&PreferClientRouting=true';
var importHeaderObj = {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'multipart/form-data; boundary="'+boundaryKey+'"',
'Content-Length': bBody.byteLength
};
var opts = {
host: 'api.powerbi.com',
path: importPath,
method: 'POST',
headers: importHeaderObj
};
var sData = '';
var req = https.request(opts, function(res) {
res.on("data", function(chunk) {
sData += chunk;
//console.log('chunk:' + chunk);
});
res.on('end', function() {
console.log('res status: ' + res.statusCode);
console.log('res end: ' + sData);
});
});
req.on('error', function(e) {
console.log('error ret: ' + e);
});
req.write(bBody);
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment