0

I'm working on a NiFi 1.15 flow where I have to send a request to a service that requires 2 pieces of form-data sent in a POST request as a multipart/form-data. The first part is a simple JSON object with a few parameters, while the second part is a CSV file. Here is an example of what I am trying to achieve.

Content-Type: multipart/form-data; boundary=1cf28ed799fe4325b8cd0637a67dc612


--1cf28ed799fe4325b8cd0637a67dc612
Content-Disposition: form-data; name="json"; filename="json"

{"Param1": "Value1","Param2": "Value2","Param3": true}
--1cf28ed799fe4325b8cd0637a67dc612
Content-Disposition: form-data; name="file"; filename="body.csv"

Field1,Field2,Field3,Field4,Field5
VALUE_FIELD_1,VALUE_FIELD_2,VALUE_FIELD_3,"Some value for field 4",VALUE_FIELD_5

--1cf28ed799fe4325b8cd0637a67dc612--

Another acceptable output would have the Content-Disposition lines empty.

Due to a few restrictions in the environment I am working on, I am not allowed to use scripting processors, such as ExecuteGroovyScript as suggested in another SO question.

Instead, I started creating a GenerateFlowFile -> InvokeHTTP flow. The GenerateFlowFile would output to a flow file a text similar to the one mentioned above. Here is the screenshot of the GenerateFlowFile.

Configuration of the GenerateFlowFile

The connected InvokeHTTP processor is configured to use the POST Http Method and to send headers (the Authorization header in my case) and Send Message Body is set to true. It also extracts the Content-Type from the flow file previsously generated attribute through a ${mime.type} function. You can see the details in the following screenshots.

enter image description here enter image description here

Sadly, this does not work. The server responds with an "Unexpected end of MIME multipart stream. MIME multipart message is not complete." error.

After searching for a while in SO, I found another question describing what looks like a similar problem, but there they are getting a different error and is also posting parameters through a different method.

I am also aware about the blog post from Otto Fowler where he shows how InvokeHTTP supports POSTs with multipart/form-data. I did try this approach too, but did not manage to get it working. The service throws an error stating that NiFi does not send one of my post:form:parts.

Right now I am stuck and am not able to send that data. I did manage to write a simple Python script to test if the server is working properly and it is. For reference, here is the script:

import requests
server = 'https://targetserver.com'

#Authentication
result = requests.post(server + '/authentication',
                       {'grant_type': 'password',
                        'username': 'username',
                        'password': 'password'})
token = result.json()['access_token']

#Build the request
headers = {'Authorization': 'bearer ' + token}
json_data = '{"Param1": "Value1","Param2": "Value2","Param3": true}'

# First the JSON then the csv file.
files = {'json': json_data,
         'file': open('body.csv', 'rb')}
result = requests.post(server + '/endpoint', headers = headers, files = files)
print(result.text)

Does anyone have a suggestion on how to get around this situation?

Ramiro
  • 698
  • 6
  • 21
  • 1
    the error `Unexpected end of MIME multipart stream. MIME multipart message is not complete.` saying that the body is incorrect. so you put something wrong into body. btw there is a smaller script to build multipart correctly: https://stackoverflow.com/questions/57122809/nifi-multipart-form/57204862#57204862 – daggett Dec 30 '21 at 20:47
  • 1
    without sharing the body we can't help. the delimiter between http headers must be CRLF - this could be an issue when using generateFlowFile – daggett Dec 30 '21 at 20:51
  • I managed to solve this issue. The problem was related with CRLF and some message formatting issue. Thanks @daggett. – Ramiro Jan 03 '22 at 11:52

0 Answers0