Support Questions

Find answers, ask questions, and share your expertise

Start a processor using Python SDK of Apache Nifi

avatar
Rising Star

Hi,
I have a python code which is trying to authenticates to my NIFI server and start a processor inside it.
Please see code below:


from flask import Flask
import nipyapi
import requests
 

app = Flask(__name__)

@app.route('/')
def home():
    processor_id = <processorID>
    nifi_general = "https://mynifiinstance/nifi-api"
    start_processor_url = f"{nifi_general}/processors/{processor_id}/run-status"
 
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    username = "username"
    password = "password"
    data = { "username": username,
            "password": password
        }
 
 
    response = requests.post(url=nifi_url, data=data,verify=False, headers=headers)
 
    if response.status_code == 200:
        # if successfully authenticated, then start the processor
        start_processor = requests.put(url=start_processor_url, headers=headers)
        print(start_processor.status_code)
        if start_processor.status_code == 200:
            return("Processor started successfully.")
        else:
            return(f"Starting processor failed. Status code: {start_processor.status_code}")
    else:
        return(f"Failed to start processor. Status code: {response.status_code}")
   
if __name__ == '__main__':
    app.run()
 
However, I am unable to authenticate with the given username and password. Note: that I have not made any changes in the nifi.properties file. Does it have to be changed?
Looking fwd to your responses.
2 REPLIES 2

avatar
Rising Star

Any feedback will be appreciated. Thanks.

avatar
Master Mentor

@Kiranq 

The NiFi rest-api endpoint for obtaining token is not rest-api/access.
It is rest-api/access/token

https://<nifi-hostname>:<nifi-port>/nifi-api/access/token 
and data passed like:
--data-raw 'username=<username>&password=<user password>'

Then the token needs to be passed with every subsequent rest-api request.

If you found any of the suggestions/solutions provided helped you with your issue, please take a moment to login and click "Accept as Solution" on one or more of them that helped.

Thank you,
Matt