One of my previous posts about NiFi dealt with POSTing dynamically built JSON. Looking at it in retrospect, I realised there was space for improvement and ended up writing a custom processor. The result is not as feature rich as NiFi’s out-of-the-box counterparts InvokeHTTP and PostHTTP. However, my implementation incorporates request body definition including the support of expression language.

Both InvokeHTTP and PostHTTP provide a great degree of flexibility. One feature they lack though is the ability to define request body. In these regards, they depend on an incoming flow file. While this is fine in general, in certain cases it creates redundancy.

Here is what I mean.

complexity_behind_http_post

In order to submit dynamically built JSON I need to declare and initialise variables, convert them into a JSON object and pass the result as a flow file content to InvokeHTTP, which in turn posts the captured data to a target URL.

Don’t get me wrong, it’s a smart concept, but inevitably, it becomes a bit too repetitive over the time.  Imagine you could get rid of data preparation steps and work directly with the post processor.

Let me give you a one-to-one comparison of my example before and after the change.

Before: Iterative POSTs, data is built separately

Before: Iterative POSTs, data is built separately

 

After: The same flow with built-in request body definition

After: The same flow with built-in request body definition

Finally, here is how the streamlined request definition looks like. Notice the use of EL in the request body.

http_post_with_body_definition

If that sounds interesting to you, please go ahead and check out the source code as well as a sample template.

Without going into details, PostHTTPWithJsonBody is streamlined for JSON based APIs and sports concise code with a minimum of dependencies.

Thanks for reading and stay tuned for my next post, where I will delve into implementation details and challenges I came across when building the processor.

 

Categories: Java

Tomas Zezula

Hello! I'm a technology enthusiast with a knack for solving problems and a passion for making complex concepts accessible. My journey spans across software development, project management, and technical writing. I specialise in transforming rough sketches of ideas to fully launched products, all the while breaking down complex processes into understandable language. I believe a well-designed software development process is key to driving business growth. My focus as a leader and technical writer aims to bridge the tech-business divide, ensuring that intricate concepts are available and understandable to all. As a consultant, I'm eager to bring my versatile skills and extensive experience to help businesses navigate their software integration needs. Whether you're seeking bespoke software solutions, well-coordinated product launches, or easily digestible tech content, I'm here to make it happen. Ready to turn your vision into reality? Let's connect and explore the possibilities together.