Skip to content

Instantly share code, notes, and snippets.

@russch
Created January 23, 2015 19:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save russch/f77f95e13d2fbc698a1a to your computer and use it in GitHub Desktop.
Save russch/f77f95e13d2fbc698a1a to your computer and use it in GitHub Desktop.
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