Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
sachin131
Regular Visitor

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 2
v-yiruan-msft
Community Support
Community Support

Hi @sachin131 ,

Whether your problem has been resolved? If yes, could you please mark the helpful post as Answered? It will help the others in the community find the solution easily if they face the same problem as yours. Thank you.

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
v-yiruan-msft
Community Support
Community Support

Hi @sachin131 ,

Please check if it hit any following limitations, you can find the details in this official documentation.

yingyinr_0-1652084461443.png

Error 404: what it means and how to fix it

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.