Nifi multipart form
Asked Answered
M

3

6

I’m trying to do a very simple multipart form post to an api. I can’t see any way of doing this in apache Nifi since it only seems to have one input for form data. There seem to be a lot of existing questions about this on here and the Nifi forum but none of them have any answers.

I’m trying to use invokehttp. Is there a way to build the multiple form data before I put it into invokehttp?

Marmion answered 20/7, 2019 at 7:52 Comment(3)
possible to use script...Nial
James, could you provide an example of incoming ff and desired outgoing ff with multipart?Nial
@Nial Something like this: curl -F person=anonymous -F [email protected] example.com/submit.cgiMarmion
N
7

You could use ExecuteGroovyScript processor with the following code to build multipart/form-data:

@Grab(group='org.apache.httpcomponents', module='httpmime', version='4.5.9')

import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.ContentType

def ff = session.get()
if(!ff)return

def multipart

ff.write{streamIn, streamOut->
    multipart = MultipartEntityBuilder.create()
        //specify multipart entries here
        .addTextBody( "username", ff.filename ) //get from flowfile attribute "filename"
        .addTextBody( "secret", new File("./README").getText("UTF-8") ) //add text body from file
        .addBinaryBody( "avatar", streamIn, ContentType.DEFAULT_BINARY, ff.filename )   //add flowfile content as binary body
        .build()
    multipart.writeTo(streamOut)
}
//set the `mime.type` attribute to be used as `Content-Type` in InvokeHTTP
ff."mime.type" = multipart.getContentType().getValue()
REL_SUCCESS << ff

check the other add* methods to add multipart parameters: org.apache.http.entity.mime.MultipartEntityBuilder


To check this code I used InvokeHTTP processor just after ExecuteGroovyScript with only following parameters changed:

Nial answered 25/7, 2019 at 14:52 Comment(2)
group='org.apache.httpcomponents', module='httpmime', version='4.5.9' ... not able to find exact version.. how to find exact versionWineglass
mvnrepository.com/artifact/org.apache.httpcomponents/httpmimeNial
S
0

If you set a dynamic property on the InvokeHTTP processor with the name Content-Type and the value multipart/form-data, what is the error you get? By specifying the dynamic property, it will send that as a header Content-Type: multipart/form-data.

InvokeHTTP will simply send the flowfile content as the request body. What is the remote endpoint expecting for input, and how does this differ from what you are currently able to send?

Sunnisunnite answered 22/7, 2019 at 22:28 Comment(2)
It seems the multipart form items needs to be sent as separately defined form properties but Nifi only allows the request body to be sent as single lump.Marmion
You can just use an online end point such as webhook.site to seeMarmion
S
0

Thought I'd add an answer to this question as InvokeHTTP added a property in 1.12.0 which now makes this possible out of the box.

You can set the Content Type property to multipart/form-data

And the new property Flowfile Form Data Name allows you set the name of the property that the form content is for.

For example, if your form is expecting a property called file to contain the file upload, you would set Flowfile Form Data Name to file.

This would be equivalent to the following cURL curl -X POST <url> -F 'file=@./myfile' -H 'Content-Type: multipart/form-data'

InvokeHTTP

Socialize answered 22/9, 2021 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.