如何使用Apache-nifi的InvokeHttp处理器发送带有附件的http POST multipart / form-data请求?

时间:2019-05-09 15:00:45

标签: http http-post multipartform-data apache-nifi

我正在尝试使用Nifi的InvokeHTTP处理器向POST API发送一个多部分/表单数据请求。该请求需要一个json和一个文件。 POSTMAN中的请求标头和请求正文看起来像这样-

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]

文件对象包含我要发送的文件详细信息。

我想在nifi中发送此多部分/表单数据请求。根据我在一个论坛上看到的答案(很抱歉,没有链接),我试图在将流程文件发送到InvokeHttp处理器之前,使用ReplaceText处理器在流程文件的内容中创建此请求正文。流文件的内容看起来像这样-

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--

这似乎没有用,对我来说似乎不对。我是Nifi的新手。谁能帮助我了解我在做什么错,或者提供一些有关如何正确处理此问题的见解?谢谢!

我改为尝试使用ExecuteStreamCommand处理器来简单地运行带有命令参数的curl命令-

-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";

这可行,但是我想知道如何使用InvokeHttp处理器来做到这一点。任何帮助是极大的赞赏!谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用 GenerateFlowFile-> InvokeHTTP 来实现。

GenerateFlowFile 将在“自定义文本”字段中创建帖子的有效负载(在您的情况下为localsystem.HL7的内容)。

InvokeHTTP 必须将Content-Type设置为$ {mime.type}-默认值和

然后在POST之后查看输出是什么 enter image description here