Nifi多部分形式 [英] Nifi multipart form

查看:93
本文介绍了Nifi多部分形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对api做一个非常简单的多部分表单发布.我在apache Nifi中看不到任何方法,因为它似乎只有一个表单数据输入.在这里和Nifi论坛上似乎有很多关于此的现有问题,但是它们都没有任何答案.

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.

我正在尝试使用invokehttp.在我将其放入invokehttp之前,是否可以构建多种表单数据?

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

推荐答案

您可以使用带有以下代码的ExecuteGroovyScript处理器来构建multipart/form-data:

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

检查其他add*方法以添加多部分参数:

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

要检查此代码,我仅在ExecuteGroovyScript之后使用了InvokeHTTP处理器,仅更改了以下参数:

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

这篇关于Nifi多部分形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆