Created on
11-04-2019
11:48 AM
- last edited on
11-04-2019
01:06 PM
by
Robert Justice
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?