Forum Discussion
nfadili
6 years agoFrequent Visitor
'Post in Group' API using Nodejs
Hello. I am implementing a feature that allows a user to upload a .pbix file from their local machine through our webpage and have it published into a Power BI workspace by our web app's server. The ...
dpiret
Helper I
5 years agoHi nfadili
I made it work, but I'm unsure whether it will help you.
I used form-data's async getLength() function, which returns the length of the stream. This length is not exactly the file's lenght you obtain from the disk:
- fs.statSync(__basedir +'/pbix/workspace-master.pbix').size: 199460
- from getLenght: 199672
A crude working version looks like this
try {
let AccessToken = await auth.getMicrosoftToken();
var formData = new FormData();
formData.append(datasetDisplayName,fs.readFileSync(__basedir +'/pbix/workspace-master.pbix'));
var options={};
options.method= 'POST';
options.data= formData;
options.url= Mustache.render("https://api.powerbi.com/v1.0/myorg/groups/{{groupId}}/imports?datasetDisplayName={{datasetDisplayName}}", { groupId: groupId, datasetDisplayName: datasetDisplayName });
var headers = formData.getHeaders();
headers['Authorization'] = `Bearer ${AccessToken}`;
headers['content-type'] = 'multipart/form-data'
formData.getLength(async function(err, len) {
headers['Content-Length']=len;
options.headers= headers;
console.log(`uploading PIX ${datasetDisplayName} to the workspace ${groupId}`)
let response = await http.axios(options)
console.log(`It may have worked\n${JSON.stringify(response.data)}`);
return response.data
});
I guess I'll see you around 😉
d