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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
vaalyushin
Helper I
Helper I

Upload pbix with python

Hi! I have Power BI Report Server May 2022 and i try to upload pbix-file use api. I write this code: 

 

from asyncio.windows_events import NULL
from requests import request
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
import json
from requests_ntlm import HttpNtlmAuth
from urllib3.exceptions import InsecureRequestWarning
from urllib3 import disable_warnings

disable_warnings(InsecureRequestWarning)

urlpbirs = "https://pbirs.ontoso.com/reports/api/v2.0/PowerBIReports"
file = open("testapi.pbix", "rb")
file_location = "testapi.pbix"
files = {'value': (None, open(file_location,
                   'rb'), 'multipart/form-data')}
mp_encoder = MultipartEncoder(fields=files)

headers = {
    'accept': 'application/json',
    # Already added when you pass json= but not when you pass data=
    # 'Content-Type': 'application/json',
}

json_data = {
    'Name': 'test',
    'Description': 'test api with python',
    'Type': 'PowerBIReport',
    'Hidden': False,
    'IsFavorite': False,
    'ContentType': NULL,
    'Content': '',
    'HasDataSources': True,
    'Path': '/BI Test'
}

r = requests.post(url=urlpbirs, data=mp_encoder, auth=HttpNtlmAuth(
    'user', 'pass'), verify=False, headers=headers, json=json_data)

But it doesn't work

 

2 REPLIES 2
d_gosbell
Super User
Super User

I'm pretty sure you need to use the CatalogItems endpoint to upload reports.

eg.

from asyncio.windows_events import NULL
from requests import request
import requests
import base64
import json
from requests_negotiate_sspi import HttpNegotiateAuth
from urllib3.exceptions import InsecureRequestWarning
from urllib3 import disable_warnings

disable_warnings(InsecureRequestWarning)

urlpbirs = "http://localhost/reports/api/v2.0/CatalogItems"
file = "C:/temp/test.pbix"
file_text = open(file, 'rb')
file_read = file_text.read()
file_encode = base64.b64encode(file_read).decode('utf-8')

headers = {
    'accept': 'application/json',
    'Content-Type': 'application/json',
}

json_data = {
    'Name': 'BI Test 2',
    '@odata.type': '#Model.PowerBIReport',
    'ContentType': '',
    'Content': file_encode,
    'Path': '/BI Test 2'
}

r = requests.post(url=urlpbirs,  auth=HttpNegotiateAuth(), verify=False, headers=headers, json=json_data)
print(r)

This code works well?

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.