首页 > 解决方案 > 如何使用 Apache-nifi 的 InvokeHttp 处理器发送带有附加文件的 http POST multipart/form-data 请求?

问题描述

我正在尝试使用 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 中发送这个 multipart/form-data 请求。根据我在其中一个论坛上看到的答案(抱歉没有指向它的链接),我试图在将流文件发送到 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 处理器来做到这一点。任何帮助是极大的赞赏!谢谢你。

标签: httphttp-postmultipartform-dataapache-nifi

解决方案


您可以使用GenerateFlowFile --> InvokeHTTP来做到这一点。

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

InvokeHTTP需要将 Content-Type 设为 ${mime.type} - 默认值和

然后看看你的 POST 之后的输出是什么 在此处输入图像描述


推荐阅读