Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

How to send an http POST multipart/form-data request with a file attached using Apache-nifi's InvokeHttp processor?

How to send an http POST multipart/form-data request with a file attached using Apache-nifi's InvokeHttp processor?

New Contributor

I am trying to send a multipart/form-data request to a POST API using Nifi's InvokeHTTP processor. This request takes a json and a file. The request headers and request body in POSTMAN look something like this -


POST /delivery/deliverPackage
User-Agent: PostmanRuntime/7.6.1
Accept: */*
Host: example.hostname:port
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------161413078116998145311888
content-length: 1115
json={ "destinationProtocol" : "HL7", "destinationFormat": "HL7_V2_ORU", "destinationType": "example", "destinationConnectionParams":{ "URI": "example", "HOST": "example", "PORT": "example" } }file=[object Object]


where the file object contains the file details I am trying to send.


I want to send this multipart/form-data request in nifi. Based on an answer I saw on one of the forums (sorry do not have the link to it), I am trying to create this request body in the content of the flowfile using ReplaceText processor before sending the flowfile to an InvokeHttp processor. The flowfile content looks something like this -


POST /delivery/deliverPackage
User-Agent: curl/7.46.0
Accept: */*
Host: example.hostname:port
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------161413078116998145311888
content-length: 1115

--------------------------161413078116998145311888
Content-Disposition: form-data; json="{ "destinationProtocol" : "HL7", "destinationFormat": "HL7_V2_ORU", "destinationType": "example", "destinationConnectionParams":{ "URI": "example", "HOST": "example", "PORT": "example" } }"

anonymous
--------------------------161413078116998145311888
Content-Disposition: form-data; name="file"; filename="/path/to/file/in/localsystem.HL7”
Content-Type: text/plain

contents of the file
--------------------------161413078116998145311888--


This doesn't seem to be working though, it doesn't look right to me. I am pretty new to Nifi. Can anyone help me understand what I'm doing wrong or give some insights on how to handle this properly? Thanks!


I have instead tried to use an ExecuteStreamCommand processor to simply run the curl command with command arguments -


-X POST;"https://example.hostname:port/delivery/deliverPackage?json=%7B%20%22destinationProtocol%22%20%3A%20%22HL7%22%2C%20%22destinationFormat%22%3A%20%22HL7_V2_ORU%22%2C%20%22destinationType%22%3A%20%22example%22%2C%20%22destinationConnectionParams%22%3A%7B%20%22URI%22%3A%20%22example%3A%2F%2Fexample%3A15050%22%2C%20%22HOST%22%3A%20%22example%22%2C%20%22PORT%22%3A%20%22example%22%20%7D%20%7D";-H "Content-Type: multipart/form-data";-F "file=@/path/to/file/in/localsystem.HL7";


This works, but I wanted to know how to do it using the InvokeHttp processor. Any help is greatly appreciated! Thank you.


7 REPLIES 7
Highlighted

Re: How to send an http POST multipart/form-data request with a file attached using Apache-nifi's InvokeHttp processor?

New Contributor

Has anyone been successful yet with POST multipart via invokehttp? [We are running 1.9.2.]

Highlighted

Re: How to send an http POST multipart/form-data request with a file attached using Apache-nifi's InvokeHttp processor?

Rising Star

Hi @ankita_pise , 

Were you able to achieve this using InvokeHttp processor ?

I am also looking for the solution.

 

Thanks,

Mahendra

Highlighted

Re: How to send an http POST multipart/form-data request with a file attached using Apache-nifi's InvokeHttp processor?

Explorer

Add me to this growing list.

Highlighted

Re: How to send an http POST multipart/form-data request with a file attached using Apache-nifi's InvokeHttp processor?

New Contributor

Any luck on this?

i am trying to POST multiple files to API

curl -X POST "https://xxxxxx/xxxxx" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "file1=@File1.csv;type=application/vnd.ms-excel" -F "file2=@File2.txt;type=text/plain" -F "file3=@File3.csv;type=application/vnd.ms-excel" -F "format=flat"

Highlighted

Re: How to send an http POST multipart/form-data request with a file attached using Apache-nifi's InvokeHttp processor?

Master Collaborator

@ankita_pise Did you try appending the json string of values to the Url and like you did in curl?  

I believe this may work if it works in Curl.

 

Let us know the results.

 


 


If this answer resolves your issue or allows you to move forward, please choose to ACCEPT this solution and close this topic. If you have further dialogue on this topic please comment here or feel free to private message me. If you have new questions related to your Use Case please create separate topic and feel free to tag me in your post.  


 


Thanks,



Steven

Highlighted

Re: How to send an http POST multipart/form-data request with a file attached using Apache-nifi's InvokeHttp processor?

New Contributor

Anyone having the solution for this we are struggling lot many days!!!

 

We need to send file in multi-part/formdata...

Highlighted

Re: How to send an http POST multipart/form-data request with a file attached using Apache-nifi's InvokeHttp processor?

Rising Star

I could not succeed in multipart upload with InvokeHTTP processor.
So I used a simple java code to upload file using 'multipart/form-data'.
Invoked java code from nifi SprintContextProcessor.
As my file was large, I wrote the file content to disc before invoking 'SprintContextProcessor' and using java code read the file content and uploaded to the target.

You can transfer flow file content directly to the java code (SpringContext processor).

 

Thanks

Mahendra

Don't have an account?
Coming from Hortonworks? Activate your account here