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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Anonymous
Not applicable

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

Anonymous
Not applicable

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

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.