Name of attribute for “Put Response Body In Attribute” in invokeHTTP - apache-nifi

I have a endpoint would return response as follow.
{
"result": [
{},
....
{}]
}
I am trying to use invokeHTTP and enable “Put Response Body In Attribute” to keep origin flowfile and response from api.
but it seems add a attribute named $.result as follow
Is there any way to set a proper name for result attribute ? Thanks.

You try to extract results using a JSON path. However this is not possible from within InvokeHttp. You may want to use EvaluateJsonPath processor.
Documentation for Put Response Body In Attribute:
If set, the response body received back will be put into an attribute
of the original FlowFile instead of a separate FlowFile. The attribute
key to put to is determined by evaluating value of this property.
To keep the original flow file after calling InvokeHttp, you can move it to an attribute (if the content is small enough) just before calling InvokeHttp or you can use MergeContent with original flow file and response flow file from InvokeHttp.

Related

Nifi get post body params as attributes

I used HandleHttpRequest and can handle POST and GET methods. I used a AttributesToJson processor after it and I need to get POST body parameters as an attribute to include in the second processor. With GET everything is ok since the parameters already existed in http.header.xxx attribute.
Is there any way to extract POST body parameters as attributes?
EDIT-1:
post body example:
m1=7b45336b-48d2-4339-bb96-14610733df7d&ht=1655125815&on=185fcb1d55c40a6dccd68852202d9417&lp=418cb87b-47aa-4f59-817f-d227dabe7219&...
I can see the HandleHttpRequest processor has Parameters to Attributes List which say it creates a comma-separated parameters or form-data as attribute. But it just works for GET parameters not POST body.

Passing JSON Header and Body to subsequent InvokeHTTP Processor in ApacheNifi

I am trying to build an automated flow to export profiles from a SaaS cloud platform where I have all the REST APIs. Here is what I am doing
Login to login API to get token (I have been able to successfully do that using InvokeHttp processor). I get the token in the response JSON as follows
{ "access_token" : "<some_token_value>", "links" : [ { "rel" : "self", "href" : "https://ccadmin-xxxx.oracleoutsourcing.com/ccadmin/v1/login" } ], "token_type" : "bearer", "expires_in" : 300 }
I extract the token from the JSON using EvaluateJSONPath and add dynamic property "token" to map to access_token of the JSON response I got.
The output of evaluateJsonPath is an attribute "token" which has the value of access_token from step 1.
Now i am preparing a BODY for the next POST call, so i use UpdateAttribute processor to define my attributes.
Next i convert the attributes that i define in the previous step to JSON using AttributesToJSON processor. This adds the following to the attributes path in its output
JSONAttributes
{"mode":"standalone","filename":"profiles.json","format":"json","id":"Profiles"}
Also the token from step 1 still flows as an attribute
Next i pass all this to my 2nd InvokeHttp processor to call the bulk export.
Now, I want to pass this token as the Header and also pass the JSON body to the Invoke HTTP processor. So i just make the following change. Add a dynamic attribute to the InvokeHTTP called "Authorization" and pass value as Bearer ${token} and i believe the body will automatically be taken from the attributes from previous processor.
But the call fails. Is there anything i am missing. I have tried running this through POSTMAN and everything runs fine there.
Nifi Flow Diagram
Input to 2nd InvokeHttp Processor
Second InvokeHttp POST processor
I think i got the issue. i see all the things are flowing as attributes in the flow. The body needs to flow as content. so i cut short the UpdateAttribute and AttributeToJSON and instead put a ReplaceText processor and put the JSON body there. And i am able to make a successful call now.

How to route original dataflow if JSON response from InvokeHTTP contains value

I want to send a dataflow to a webservice which will then respond based on the content of the dataflow. So it might respond with a JSON that says {'Errors':'None'} or {'Errors':'5'}. I would like to then use this information to continue processing the original dataflow if no errors are found, orlog the information if any errors are found. My question is, how can I route based on the values in the JSON response?
You can use EvaluateJsonPath to get the value of Errors into an attribute, then RouteOnAttribute to send the different values down different paths.
Alternatively you could use QueryRecord with queries like SELECT * FROM FLOWFILE WHERE Errors = 'None'. Each query gets its own downstream relationship, so it has the effect of routing in this case.

In NiFi processor 'InvokeHTTP' where do you write body of POST request?

Before posting this question about Apache NiFi InvokeHTTP I have gone through all other questions and their answersbut I am still unsure the best flow I should have. My situation is as below:
1) From Apache Kakfa, I get raw metadata.
2) Using EvaluateJSONPath I get attribute I want.
3) Using RouteOnAttribute I created 3 routes based on the attribute value I got from step-2 above.
4) Now based on the attribute value I want to decide whether I should go for GET or for POST or for Delete.
5) My question is where/how to set POST message? GET message? Delete Message body?
6) I am able to set the URL in configuration part provided by InvokeHTTP. But message body I don't know which is that property? or its in flow file using ReplaceText?
I read somewhere that before you divert your Restful POST HTTP request to InvokeHTTP you must have another processor before which changes the content of flow file.
Ref: Configuring HTTP POST request from Nifi
Please help. Thanks.
regards,
Yeshwant
Adding on to what Bryan had explained, POST will use the FlowFile content as the message body so if you have some other data which you want to wipe/transform into something and then sent as the message body, you can leverage the following processors :
ExtractText to read data from the existing FlowFile content
ReplaceText to erase the existing content of the FlowFile and replace it with different one
To set the headers for the REST calls, InvokeHTTP has the property Attributes to Send property which takes a regex which will scanned against the incoming FlowFiles' attributes and whichever attributes are matched are taken and sent as HTTP header.
To add new attribute to your existing FlowFile, you can use UpdateAttribute
For a POST, the body will be whatever is in the flow file content.
a GET and DELETE typically wouldn't have a body since the information would typically be provided in the URL or query params.

Block (or strip out) FlowFile Content

I need to perform an HTTP Post from NiFi, but I don't want/need the request to carry all of the FlowFile's content.
Is there some way to pass attributes of a FlowFile but not the full content?
If the request body of your Http Post is JSON, you can use the AttributesToJSON Processor which allows you to pick which attributes you want to include in the resulting JSON. You can then configure the processor so the resulting JSON overwrites the existing flowfile content.
Keep in mind that the resulting JSON will be flat so you may need to transform it to the expected format. For that, you can use the JoltTransformJSON Processor.
Below is an example of what your dataflow might look like. I hope this helps!

Resources