Support Questions
Find answers, ask questions, and share your expertise

ExecuteScript Processor - InputStream not being sent to web API

ExecuteScript Processor - InputStream not being sent to web API

New Contributor

I have a Python script within an ExecuteScript processor and am trying to upload the contents of the flow file to an API that expects multipart/form data.  The script is using the requests object to post the request; however, the contents of the flow file does not get sent to the API.  Below is the script:

 

 

import sys
sys.path.append('/usr/lib/python3/dist-packages/')
import json
import requests
from org.apache.nifi.processor.io import InputStreamCallback
from org.apache.commons.io import IOUtils

# Define a subclass of InputStreamCallback for use in session.read()
class PyInputStreamCallback(InputStreamCallback):
  def __init__(self):
        pass
  def process(self, inputStream):
        length = flowFile.getSize()
        log.warn("MyLog >> File length: " + str(length))
        multipart_form_data = {'file': ('test.xml', inputStream, 'text/xml')}
        requests.post('http://url/api/uploadfile', files=multipart_form_data)
# end class

flowFile = session.get()
if(flowFile != None):
    session.read(flowFile, PyInputStreamCallback())
    session.transfer(flowFile, REL_SUCCESS)
    session.commit()

 

 

I have verified that the inputStream has data, but the data is not sent to the API when called within NiFi.  If I run the script outside of NiFi and just read the file from the file system, it works correctly.

 

Is there something else that needs to be done to send the flowFile?

Don't have an account?