Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

How to upload template in Apache Nifi using Python request

avatar
New Contributor

The Error getting: ('Unauthorized', 401)
I tried this method according to the nifi documentaion but getting this

The Python Code Metioned Below

import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class PyNifi:
def __init__(self, host: str, port: str, username: str, password: str):
self.host = host.strip()
self.port = port.strip()
self.username = username.strip()
self.password = password.strip()
self._Secure_Authorization_Bearer = None
self.__access_status__ = None
self.__Secure_Request_Token = None

def __str__(self):
self.__access_status__ = self.__access__()
return self.__access_status__

def __access__(self):

if self.__access_status__:
return self.__access_status__

if not self._Secure_Authorization_Bearer or not self.__Secure_Request_Token:
self.access_token()

access_url = f"https://{self.host}:{self.port}/nifi-api/access"

cookies = {
'__Secure-Request-Token': self.__Secure_Request_Token,
'__Secure-Authorization-Bearer': self._Secure_Authorization_Bearer
}

response_ = requests.request("GET", access_url, cookies=cookies, verify=False)
return response_.text

def get_template_details(self):

self.access_token()

access_url = f"https://{self.host}:{self.port}/nifi-api/flow/templates"

headers = {'Content-Type': 'multipart/form-data'}

cookies = {
'__Secure-Request-Token': self.__Secure_Request_Token,
'__Secure-Authorization-Bearer': self._Secure_Authorization_Bearer
}

response_ = requests.request("GET", access_url, cookies=cookies, headers=headers, verify=False)
return response_.json()

def access_token(self):

if self._Secure_Authorization_Bearer:
return self._Secure_Authorization_Bearer

token_url = f"https://{self.host}:{self.port}/nifi-api/access/token"
payload = f'username={self.username}&password={self.password}'

headers = {'Content-Type': 'application/x-www-form-urlencoded'}

response_ = requests.request("POST", token_url, headers=headers, data=payload, verify=False)

self._Secure_Authorization_Bearer = response_.text
self.__Secure_Request_Token = dict(response_.cookies).get('__Secure-Request-Token')

return response_.text

def replace_template(self, id_: str):

access_url = f"https://{self.host}:{self.port}/nifi-api/process-groups/replace-requests/{id_}"

headers = {
# 'Content-Type': 'multipart/form-data',
'__Secure-Request-Token': self.__Secure_Request_Token,
'__Secure-Authorization-Bearer': self._Secure_Authorization_Bearer
}

response_ = requests.request("GET", access_url, headers=headers, verify=False)
return response_.text

def upload_template(self, group_id: str, template_path: str):

if not self._Secure_Authorization_Bearer:
self._Secure_Authorization_Bearer = self.access_token()

upload_url = f"https://{self.host}:{self.port}/nifi-api/process-groups/{group_id}/templates/upload"

headers = {
'Content-Type': 'multipart/form-data',
'__Secure-Request-Token': self.__Secure_Request_Token,
'__Secure-Authorization-Bearer': self._Secure_Authorization_Bearer
}

with open(template_path, 'rb') as fp:
binary_file = fp.read()

template = {"template": binary_file}

response_ = requests.request(
"POST",
upload_url,
headers=headers,
files=template,
verify=False
)
return response_.text, response_.status_code


nifi_api = PyNifi(
host='localhost',
port='8443',
username='leearun0',
password='asdh54f54fs64df546f'
)

template = nifi_api.get_template_details()

group_id = template.get("templates")[0].get("template").get("groupId")
template_path = '/home/arunkrishnan/Downloads/dataflow-minio-temp.xml'

response = nifi_api.upload_template(
group_id=group_id,
template_path=template_path
)


print(response)
print(template)
print(group_id)

The OutPut of the code given above

('Unauthorized', 401)

{'templates': [{'id': '084f477f-4fdf-4ce1-b709-958d90f4eef0', 'permissions': {'canRead': True, 'canWrite': True}, 'template': {'uri': 'https://localhost:8443/nifi-api/templates/084f477f-4fdf-4ce1-b709-958d90f4eef0', 'id': '084f477f-4fdf-4ce1-b709-958d90f4eef0', 'groupId': '4338f14c-0189-1000-2cb1-5236335cf37a', 'name': 'Training-01-PDP-data-to-plpgsql', 'description': 'insert product data into postgres database in local machine', 'timestamp': '07/12/2023 14:13:33 IST', 'encoding-version': '1.3'}}], 'generated': '19:18:04 IST'}

4338f14c-0189-1000-2cb1-5236335cf37a

The response of the uploading function is : ('Unauthorized', 401)
anyone experianced before these like of response??

 

0 REPLIES 0