0
votes

So I'm using powershell to automate my power BI reports into my workspace through imports API. here's the code which I'm using :

    $PBIXuri="my azure blob storage url",
    $Workspaceurl="https://api.powerbi.com/v1.0/myorg/groups/mygroups/imports?datasetDisplayName=myreportname",
    $AccessToken="my oauth token"

    #download my pbix file from azure blob storage
    $cli = New-Object System.Net.WebClient
    $exportpbix = $cli.DownloadData($PBIXuri)

    $powerBiBody = @'
    --exptest
    Content-Disposition: form-data; name="file";
    Content-Type: application/x-zip-compressed

    {0}
    --exptest--

    '@

    #set my pbix file into request body
    $encoding = [System.Text.Encoding]::GetEncoding('iso-8859-1')
    $body = $powerBiBody -f $encoding.GetString($exportpbix)

    $headers = @{
    "Authorization" = "Bearer " + $AccessToken}

    Invoke-RestMethod -Uri $Workspaceurl -Method Post -Headers $headers -Body $body -ContentType "multipart/form-data; boundary=--exptest"

but it always return an error like :

Invoke-RestMethod :     {"error":
     {"code":"MultiPartMimeStreamFormatException","pbi.error": 
       {"code":"MultiPartMimeStreamFormatException",
        "parameters":{},
        "details":[],
        "exceptionCulprit":1
        }
      }
    }

where do I went wrong on my code?

2

2 Answers

0
votes

To import a file, request Headers should include Content-Type: multipart/form-data with the file encoded as form data in the request body, as noted in the documentation. The request body should look like this:

enter image description here

See Upload a local PBIX file using the import API.

Maybe the easiest way is to try to save the downloaded data to a file on disk, let's say in $filePath, and add -InFile parameter to your your call:

Invoke-RestMethod -Uri $Workspaceurl -Method Post -InFile $filePath -ContentType "multipart/form-data"
0
votes

I've found the problem. on my request body, the content-type which I set was "application/x-zip-compressed". while my pbix file is "application/stream-octet", after I change it my files was imported correctly.