nifi invokehttp post complex json - json

I trying to use InvokeHttpProcessor in Apache NiFi to perform POST request with complex JSON body.
Accordingly this tutorial: http://www.tomaszezula.com/2016/10/30/nifi-and-http-post-configuration
I know how to use UpdateAttribute processor to add name/value pairs and then apply an additional transformation via AttributesToJSON.
But how to deal with complex JSON?
For example I have to perform request to GoogleAnalytics reporting API, so I need to perform this request:
POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
"reportRequests":
[
{
"viewId": "XXXX",
"dateRanges": [{"startDate": "2014-11-01", "endDate": "2014-11-30"}],
"metrics": [{"expression": "ga:users"}]
}
]
}
any ideas?

You can use the GenerateFlowFile and ReplaceText processors to provide a template as the flowfile content and then populate the actual values. Once that JSON object is formed as flowfile content, it should be easy to send it via POST using InvokeHTTP

Related

Jmeter JSON Format Post Processor breaks Token retrieval

I am working with Jmeter and i want to format the JSON results. So i installed JSON Format Post Processor and added it to the root of the project.
It works correctly, it formats the JSon response for easy readability. However there is also a request with requires Token authentication. It does not work with the JSON Format Post Processor.
Example:
Correct coin retrieval without JSON Format Post Processor:
{"access_token":"Secret","expires_in":3600,"token_type":"Bearer"}
And with:
{
"access_token": "Secret",
"expires_in": 3600,
"token_type": "Bearer"
}
I use a regulair extractor to extract the token: access_token":"([a-zA-Z0-9.\-\_]*)
This seems to break in combination with the formatter. No valid token access is denied in all requests
Is there an easy way around this? It seems i can either use Jmeter JSON Format Post Processor for my complete project, or not at all.
Anyone know how i deal with this?
If it is possible, apply JSON Format Post Processor after regex extractor. This could help.
For easier access to data in JSON format you can use JSON Path Extractor or SmartMeter's Boundary Body Extractor
To extract JSON data the best option is native JSON Extractor:
http://jmeter.apache.org/usermanual/component_reference.html#JSON_Extractor
Regarding your precise issue, a better regexp would be:
access_token":"([^"]+?)"
It seems that you have optional white space after :
Regular expression:
access_token":(\s*)"([a-zA-Z0-9.\-\_]*)
Template change to use $2$

How to update json field in Firebase DB with JMeter HTTP Request

I'm working with JMeter to make some HTTP Requests to my Firebase Database. I am able to create json data with a regular request, as well as with a CSV file. I'm wondering if it's possible to update, or add to, a json object.
My json data looks something like what is below. Let's say I wanted to add a boolean node called "sold", to which I could make equal to true or false. Could I create it within that json object? If so, could I also make it so that only fields with a specific "name" get updated?
{
"Price": "5.00",
"name": "buyer#gmail.com",
"seller_name": "seller#gmail.com",
"time": 1496893589683,
}
Looking into Updating Data with PATCH chapter of Saving Data article you can update a single field using HTTP PATCH Method.
JMeter supports HTTP PATCH method since version 2.8 so you should be in a position to use it as well in your test.

json conversion issue in wso2 API Manager

We are trying to use wso2 api manager to access some of our RESTful services.
The rest services expect a json payload. When json request is sent to api manager it converts the json request to xml and again while forwarding to the actual endpoint it converts to json back.
The resulting json is not matching exactly the original request in one of our cases.
For instance if the rquest contains an array of elements and if only one element is passed in the array then when api manager forwards the request to the endpoint the array characters ([,]) are removed.
eg.
our original request was
{
"entities": [
{
"name":"KK71CP20000523A1",
"descr":"VaS",
"mnf":"BCT",
"mdlyr":"2012"
}
]
}
the request sent by api manager was
{
"entities":
{
"name":"KK71CP20000523A1",
"descr":"VaS",
"mnf":"BCT",
"mdlyr":"2012"
}
}
The array wrapping is removed under entities element.
When the number of elements is more than one then the array characters are retained.
We faced the same issue in ESB as well previously. But we worked around the issue by extending the default JSONMessageFormatter and using the seriliazeAsArray method available in the jettison library.
But we dont want to do this customization in API Manager.
Is there a better way of fixing this issue? Any patch available from wso2 to fix this?
All carbon products comes with same message builders and formatters, for your case can you check with JSONStreambuilder and formatter.
Look at this reference,

Using Spring have multiple JSON request (POST) in a single HTTP call

How can I use Spring3 framework when combining 2 different REST (using JSON) requests within a single HTTP call? I am using Spring with jackson for JSON conversion. What is the most optimal way of doing it? I am not sure, if it comes under multi-part request.
For example, if I had the following 2 unrelated JSON payloads that I want to combine in a single HTTP call.
"json1":
{
"Name": "abc",
"Age": "111"
}
"json2":
{
"stockName": "xyz",
"stockSymbol": "SS"
}
Once my main controller method handles the initial call, is it then possible to map different additional controller methods for different JSON payloads? I want to understand what is the optimal way of handling such scenarios?
Thanks for the help.
You have to create separate resources and call separately, that is the best approach. In the calling place you can combine both JSON output. Right now you are trying like to have a method with two different different return types

WCF Data Service - JSON response has extra "results" section

Using the WCF Data Services Toolkit, or the other ways to support the $format param such as JSONPSupportInspectorAttribute I am getting some json responses that are a little odd to me.
Why does my json look like:
{
"d" : {
"results": [
{
"__metadata": {
When the json from OData.org $format is as follows:
{
"d" : [
{
"__metadata": {
Why does mine have an extra "results" sub section?
My data service is just built directly onto my entity framework model. Do I have to use a specific context template for this to go away?
This is versioning. In V2 we added the server driven paging and inline count features which need to store additional metadata on the feed. But since the feed in V1 was just a JSON array, there was no place to put such metadata.
So in V2 all feeds in responses are wrapped in "results" wrapper. That is the feed is a JSON object which has a property called "results" which has the array. There might be additional properties on the feed object (next link, count, ...).
The versioning of the payload is based on the minimum version required by any feature in that payload. So if your service is using something which requires payload of version higher than V1, the entire payload will be written using that higher version.
You can see this even on the odata.org service - try this:
http://services.odata.org/OData/OData.svc/Products?$inlinecount=allpages&$format=json
The response will be V2 and will use the results wrapper.