Reputation: 829
I want to import local pbix fle to created workspace in azure power bi account. I already created workspaceId using REST API. However when i try to import pbix file that gives 200 ok status instead of 202 accepted response with Id.
Here is the reference code i followed enter link description here
POST https://api.powerbi.com/v1.0/collections/mypbiapp/workspaces/32960a09-6366-4208-a8bb-9e0678cdbb9d/imports?datasetDisplayName=mydataset01 Authorization: AppKey MpaUgrTv5e... Content-Type: multipart/form-data; boundary="A300testx"
--A300testx Content-Disposition: form-data
{the content (binary) of .pbix file} --A300testx--
I used php curl request to called Rest API and below shows the code which i tried,
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.powerbi.com/v1.0/collections/XXXXXX/workspaces/XXX-XXX-XXX-XXXXXXXX/imports?datasetDisplayName=mydataset01');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postData = array(
'datafile' => '@C:\Users\Desktop\report1.pbix',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
"Authorization: AppKey R97v4Fe5=="
) );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
echo $response = curl_exec($ch);
curl_close ( $ch );
As a response I'm getting status code as 200 ok with json
{"id":"0331a80d-6f23-4626-9624-1f6b98ce373a"}
However this new dataset was not created in workspaceID. Please help me to find the issue here.
Upvotes: 0
Views: 1166
Reputation: 57
Here is a ready to use version of the given solution
public function import($access_key, $workspace_collection_name, $workspace_id, $file_path, $file_name, $display_name, $nameConflict = 'Overwrite')
{
$boundary = "----------BOUNDARY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.powerbi.com/v1.0/collections/$workspace_collection_name/workspaces/$workspace_id/imports?datasetDisplayName=$display_name&nameConflict=$nameConflict");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postdata = '';
$postdata .= "--" . $boundary . "\r\n";
$postdata .= "Content-Disposition: form-data; name=\"" . $file_name . "\"; filename=\"" . $file_path . "\"\r\n";
$postdata .= "Content-Type: application/octet-stream\r\n\r\n";
$postdata .= file_get_contents($file_path);
$postdata .= "\r\n";
$postdata .= "--" . $boundary . "--\r\n";
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: AppKey " . $access_key,
'Content-Type: multipart/form-data; boundary=' . $boundary,
'Content-Length: ' . strlen($postdata)
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
if (curl_error($ch)) {
return 'curl error';
}
curl_close($ch);
return $response;
}
Upvotes: 0
Reputation: 829
Here is the answer for multipart form-data post with php curl, This code works for me
$name = 'report1';
$file = 'report1.pbix';
$boundary = "----------BOUNDARY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.powerbi.com/v1.0/collections/XXXXXX/workspaces/XXX-XXX-XXX-XXXXXXXX/imports?datasetDisplayName=mydataset01');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postdata .= "--" . $boundary . "\r\n";
$postdata .= "Content-Disposition: form-data; name=\"" . $name . "\"; filename=\"" . $file . "\"\r\n";
$postdata .= "Content-Type: application/octet-stream\r\n\r\n";
$postdata .= file_get_contents($file);
$postdata .= "\r\n";
$postdata .= "--" . $boundary . "--\r\n";
curl_setopt($ch, CURLOPT_POSTFIELDS, $$postdata);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
"Authorization: AppKey R97v4Fe5==",
'Content-Type: multipart/form-data; boundary='.$boundary,
'Content-Length: ' . strlen($postdata)
) );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
echo $response = curl_exec($ch);
curl_close ( $ch );
Upvotes: 2