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.
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.
Finally, here is how the streamlined request definition looks like. Notice the use of EL in the request body.
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.