Python: download a report from SQL Server Reporting Services with requests

Question:

I’m trying to get a report from SQL Server Reporting Services with the py package requests, but I’m getting the 401 error. If I access the SQL server via web, there is a pop-up asking me for user/pwd, I guess I’m failing to replicate this step via Python code.

These don’t work:

url = "report-url"

requests.get(url, auth=(usr,pwd))

session = requests.Session() 
session.auth = (usr,pwd)
session.get(url)
Asked By: mdp

||

Answers:

There’s a related thread on Microsoft Q&A discussing accessing SSRS reports over the network
Here’s another ref about SSRS URL access
Since your sample code only implemented basic auth with the HTTP GET method I highly recommend reading the above threads cause it might be a problem related to the environment.
If you want to take care of the pop-up, you can ref this answer about implementing a headless browser or WebKit

Answered By: ShiriNmi

SSRS uses NTML auth method. By default SSRS does not allow username and password.

This code snippet include the library required.

import os
import requests
from requests_ntlm import HttpNtlmAuth

# Variables
username = os.environ.get("USERNAME")
password = os.environ.get("PASSWORD")
domain = os.environ.get("DOMAIN")
cert_path = os.environ.get("CERT_PATH")

# Create a session with NTLM authentication
session = requests.Session()
session.auth = HttpNtlmAuth(f'{domain}\{username}', password)
session.verify = cert_path #if you have HTTPS
Answered By: Fabio Ramos