0

I am trying to create a flow in Apache Nifi - Situation is like this 1- Consume message from kafka (DONE) 2- transform to a valid request body (DONE) 3- Preserve the transformed message 4- Generate a oauth2 token (it is a post api ) 5- Invoke a post api with transformed json as request body and send token in header

I have tried few ways like putting transformed json in cache and getting it after token generation but the problem is I am not able to pass token to fetchdistributedcache processor . I tried putting json file on my server but getfile doesn't take any input so not able to trigger it after getting token .

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • 1
    I believe your auth token has some time to live value. Let's say 1hour. It means you should not request it for every kafka message. So, you could create a mini-flow that runs every 40min (for example) that requests oauth token and stores it into distributed cache. In main flow at step 4 request the distributed cache and put token into attribute - this will not break your file content. – daggett Mar 22 '21 at 12:17
  • ^ This is a good suggestion that I didn't think of in my answer below; my answer assumes there is a good reason to generate tokens as part of the flow, e.g. messages needing unique auth tokens – Sdairs Mar 22 '21 at 13:31
  • @daggett Thanks for the suggestions ,it is working as expected. – Tahseem Ahmad Mar 24 '21 at 04:54

1 Answers1

1

If Step 4 (OAuth) is returning the token as the body, there is an option in InvokeHTTP to put the response body in an attribute rather than FF content, thus preserving original FF content.

So, you could skip step 3 completely.

Consume kafka -> transformations -> InvokeHTTP for OAuth token (with Put Response Body In Attribute) -> UpdateAttribute (optional - assuming you will need to do some formatting on the response body) -> InvokeHTTP (OAuth token is already in an attribute and the FF content is unchanged)

https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.12.1/org.apache.nifi.processors.standard.InvokeHTTP/

Sdairs
  • 1,912
  • 1
  • 13
  • 13
  • Actually generate token api needs another request body to generate token. And these apis are exposed by someone esle , i can't change it . – Tahseem Ahmad Mar 23 '21 at 04:05