Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multipart/mixed POST to Tableau's REST api via NodeJS
var FormData = require('form-data');
var fs = require('fs');
var XMLWriter = require('xml-writer');
...
var dsXml = new XMLWriter();
dsXml.startElement('tsRequest')
.startElement('datasource')
.writeAttribute('name', "file")
.startElement('project')
.writeAttribute('id', projectID)
.endElement()
.endElement()
.endElement();
console.log(dsXml.toString());
var CRLF = '\r\n';
var form = new FormData();
form.append('request_payload', dsXml.toString(), {contentType: 'text/xml'});
form.append('tableau_datasource',
fs.createReadStream('c:\\file.tde'),
{contentType: 'application/octet-stream'});
form.submit(
{
// Remove any protocol (like http://) from start of server name
host: tableauServer.replace(/.*?:\/\//g, ""),
//port: 8888, // uncomment to see your reqests in HTTP Proxies like Fiddler, Charles
path: '/api/2.0/sites/' + siteID + '/datasources?overwrite=true',
headers: {
'X-Tableau-Auth': adminAuthToken,
'Content-Type': 'multipart/mixed; boundary=' + form.getBoundary()
}
}, function(err, res) {
if (err) throw err;
console.log(res.statusCode);
var str = '';
//another chunk of data has been recieved, so append it to `str`
res.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
res.on('end', function () {
console.log(str);
});
});
res.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.