Support Questions

Find answers, ask questions, and share your expertise

Hosting an API in Nifi

avatar
Explorer

I want to host an API in Nifi using HandleHTTP processor. And i wanted to pass some dummy json data. I would like to get some information/resources about how to do it.

I am new to Nifi.

Thanks in Advance.

1 ACCEPTED SOLUTION

avatar
Super Guru

@P_Rat98 Creating an API with NiFi using HandleHttpRequest and HandleHttpResponse is something I have done quite a few times for Hortonworks and NiFI Customers.  This is a great use case for NiFi and sending receiving JSON to NiFi, processing JSON, and completing actions downstream is super easy.

Screen Shot 2020-08-27 at 8.49.45 AM.png

 

I have created a basic template for you which includes the HandleHttpRequest (inbound port 80 call) a process group for doing something with the JSON, and HandleHttpResponse (provides 200 response code) to respond to inbound call.    This is an API in the simplest form with NiFi.   Depending on your use case you can build out Process Api Request Process Group to suit your needs.   Out of the box you should be able to send to import template, add/start the StandHttpContextMap Controller Service, Start the flow, send a call to http://yourhost:80 and have JSON sitting in the bottom of the flow Success Queue.

 

You can find the template here:

https://github.com/steven-matison/NiFi-Templates/blob/master/NiFi_API_with_HandleHttpRequest_Demo.xm...

 

 

Some API suggestions:

  1. Be sure to take a look at both HandleHttp Processors for the properties you can configure.  Ports, hostname, acceptable methods, ssl, authentication, and more.
  2. If your API call does not care if the Process API Request finishes,  you can put HandleHttpResponse right after HandleHttpRequest, and let all the downstream work happen after the request/response is completed.  This is common when I expect my API to be only giving inbound data, and doesn't care what the response is (other than just 200 to know it was received).  In this case I accept the payload, return 200, and rest of the flow is decoupled from the connection.     If my processing time is lengthy I usually do this so the system initiating the api call is not left waiting.
  3. Once you have the basic framework built,  consider handling errors, and or returning different status codes as a variable (created before the response) in the Status Code for Handle Http Response.  Sometimes I even have different HandleHttpResponse at end of different flow branches.  For example:  if someone sends invalid JSON,  I return maybe 302 or 404 with the invalid error as the content body.
  4. Have fun with it.  

 

 

If this answer resolves your issue or allows you to move forward, please choose to ACCEPT this solution and close this topic. If you have further dialogue on this topic please comment here or feel free to private message me. If you have new questions related to your Use Case please create separate topic and feel free to tag me in your post.  

 

Thanks,


Steven @ DFHZ

View solution in original post

1 REPLY 1

avatar
Super Guru

@P_Rat98 Creating an API with NiFi using HandleHttpRequest and HandleHttpResponse is something I have done quite a few times for Hortonworks and NiFI Customers.  This is a great use case for NiFi and sending receiving JSON to NiFi, processing JSON, and completing actions downstream is super easy.

Screen Shot 2020-08-27 at 8.49.45 AM.png

 

I have created a basic template for you which includes the HandleHttpRequest (inbound port 80 call) a process group for doing something with the JSON, and HandleHttpResponse (provides 200 response code) to respond to inbound call.    This is an API in the simplest form with NiFi.   Depending on your use case you can build out Process Api Request Process Group to suit your needs.   Out of the box you should be able to send to import template, add/start the StandHttpContextMap Controller Service, Start the flow, send a call to http://yourhost:80 and have JSON sitting in the bottom of the flow Success Queue.

 

You can find the template here:

https://github.com/steven-matison/NiFi-Templates/blob/master/NiFi_API_with_HandleHttpRequest_Demo.xm...

 

 

Some API suggestions:

  1. Be sure to take a look at both HandleHttp Processors for the properties you can configure.  Ports, hostname, acceptable methods, ssl, authentication, and more.
  2. If your API call does not care if the Process API Request finishes,  you can put HandleHttpResponse right after HandleHttpRequest, and let all the downstream work happen after the request/response is completed.  This is common when I expect my API to be only giving inbound data, and doesn't care what the response is (other than just 200 to know it was received).  In this case I accept the payload, return 200, and rest of the flow is decoupled from the connection.     If my processing time is lengthy I usually do this so the system initiating the api call is not left waiting.
  3. Once you have the basic framework built,  consider handling errors, and or returning different status codes as a variable (created before the response) in the Status Code for Handle Http Response.  Sometimes I even have different HandleHttpResponse at end of different flow branches.  For example:  if someone sends invalid JSON,  I return maybe 302 or 404 with the invalid error as the content body.
  4. Have fun with it.  

 

 

If this answer resolves your issue or allows you to move forward, please choose to ACCEPT this solution and close this topic. If you have further dialogue on this topic please comment here or feel free to private message me. If you have new questions related to your Use Case please create separate topic and feel free to tag me in your post.  

 

Thanks,


Steven @ DFHZ