Forum Discussion

sachin131's avatar
sachin131
Regular Visitor
4 years ago

PowerBI Post Import In Group not working

Hello @All.

 

I am trying to upload one pbix report through "Post Import in Group" powerBI Rest API but getting 404 error. I followed the following link:
https://docs.microsoft.com/en-us/rest/api/power-bi/imports/post-import-in-group#post-import-example

 

But it is not helpful here. I am using following code. Please let me know if anyone can help me here.

 

 var url = $"https://api.powerbi.com/v1.0/myorg/groups/{targetWorkspaceId}/imports?datasetDisplayName={downloadedDataset.Name}&nameConflict=Overwrite";
string boundary = String.Format("----------{0:N}", Guid.NewGuid());
string contentType = "multipart/form-data; boundary=" + boundary;

using (MemoryStream ms = new MemoryStream())
{
    downloadedReportStream.CopyTo(ms);
    var file_bytes = ms.ToArray();

    byte[] formData = GetMultipartFormData(boundary, report.Name+".pbix", file_bytes);

    var httpWebResponse = PostForm(url, contentType, formData, "Authorization", String.Format("Bearer {0}", GetPBIAccessToken()));
    var responseStatusCode = httpWebResponse.StatusCode.ToString();
    Console.WriteLine("imported pbix ", responseStatusCode);
}

//private methods

private static byte[] GetMultipartFormData(string boundary, string fileName, byte[] fileBytes)
        {
            Stream formDataStream = new System.IO.MemoryStream();

            // Add just the first part of this param, since we will write the file data directly to the Stream
            string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
                boundary,
                fileName,
                fileName,
                "application/octet-stream");

            formDataStream.Write(Encoding.ASCII.GetBytes(header), 0, Encoding.ASCII.GetByteCount(header));
            // Write the file data directly to the Stream, rather than serializing it to a string.
            formDataStream.Write(fileBytes, 0, fileBytes.Length);

            // Add the end of the request.  Start with a newline
            string footer = "\r\n--" + boundary + "--\r\n";
            formDataStream.Write(Encoding.ASCII.GetBytes(footer), 0, Encoding.ASCII.GetByteCount(footer));

            // Dump the Stream into a byte[]
            formDataStream.Position = 0;
            byte[] formData = new byte[formDataStream.Length];
            formDataStream.Read(formData, 0, formData.Length);
            formDataStream.Close();

            return formData;
        }

        private static HttpWebResponse PostForm(string postUrl, string contentType, byte[] fileData, string headerkey, string headervalue)
        {
            HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;

            if (request == null)
            {
                throw new NullReferenceException("request is not a http request");
            }

            // Set up the request properties.
            request.Method = "POST";
            request.ContentType = contentType;
            //request.CookieContainer = new CookieContainer();
            request.ContentLength = fileData.Length;

            //Add header if needed
            request.Headers.Add(headerkey, headervalue);

            // Send the form data to the request.
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(fileData, 0, fileData.Length);
                requestStream.Close();
            }
            return request.GetResponse() as HttpWebResponse;
        }

 

2 Replies