Discussion:
Help: HTTPClientConnector POST JSON in script
(too old to reply)
Steven
2017-04-27 08:39:58 UTC
Permalink
Raw Message
Hi all,

I would like to ask if there is any sample to describe how to post a JSON data as a http body through HTTPClientConnector in a script. My case needs to update asset properties in QRadar through SSL and SEC token.
Here is my script for reference:

/* script */
var assetId = 1442;
var updateProperties = [];

updateProperties.push({
"type_id" : 1002,
"value" : "testing"
})

var json = { "properties" : updateProperties };
var QRadarHost = system.getTDIProperty("host");
var token = system.getTDIProperty("token");

request.setAttribute("http.url","https://"+QRadarHost+"/api/asset_model/assets/"+assetId);
request.setAttribute("http.method","POST");
request.setAttribute("http.body", json);
request.setAttribute("http.Accept","text/plain");
request.setAttribute("http.Content-Type","application/json");
request.setAttribute("http.Version","8.0");
request.setAttribute("http.cookie","null");
request.setAttribute("http.Allow-Experimental","true");
request.setAttribute("http.Allow-Provisional","true");
request.setAttribute("http.SEC", token);

response = HConn.queryReply(request);
task.dump(response);

Many thanks
Johan Varno
2017-04-28 06:12:47 UTC
Permalink
Raw Message
This is probably more than you asked for. But it's good stuff.
There's a integration solution that
1. reads events from QRadar
2. performs REST calls to Guardium
It's a complete solution, but inside it you'll find connectors that use the http client connector
There's a "download" link on this page. You might be interested in the doc as well. Good learning!

https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/W746177d414b9_4c5f_9095_5b8657ff8e9d/page/QRGuardium

- johan
s***@fusionfuzion.com
2017-04-29 02:13:01 UTC
Permalink
Raw Message
Johan Varno於 2017年4月28日星期五 UTC+8下午2時12分48秒寫道:
Post by Johan Varno
This is probably more than you asked for. But it's good stuff.
There's a integration solution that
1. reads events from QRadar
2. performs REST calls to Guardium
It's a complete solution, but inside it you'll find connectors that use the http client connector
There's a "download" link on this page. You might be interested in the doc as well. Good learning!
https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/W746177d414b9_4c5f_9095_5b8657ff8e9d/page/QRGuardium
- johan
Thanks Hohan for your reply, I will take a look.
Eddie Hartman
2017-04-28 06:46:54 UTC
Permalink
Raw Message
Post by Steven
Hi all,
I would like to ask if there is any sample to describe how to post a JSON data as a http body through HTTPClientConnector in a script. My case needs to update asset properties in QRadar through SSL and SEC token.
/* script */
var assetId = 1442;
var updateProperties = [];
updateProperties.push({
"type_id" : 1002,
"value" : "testing"
})
var json = { "properties" : updateProperties };
var QRadarHost = system.getTDIProperty("host");
var token = system.getTDIProperty("token");
request.setAttribute("http.url","https://"+QRadarHost+"/api/asset_model/assets/"+assetId);
request.setAttribute("http.method","POST");
request.setAttribute("http.body", json);
request.setAttribute("http.Accept","text/plain");
request.setAttribute("http.Content-Type","application/json");
request.setAttribute("http.Version","8.0");
request.setAttribute("http.cookie","null");
request.setAttribute("http.Allow-Experimental","true");
request.setAttribute("http.Allow-Provisional","true");
request.setAttribute("http.SEC", token);
response = HConn.queryReply(request);
task.dump(response);
Many thanks
You're so close. All you need is:

request["http.body"] = toJson(json); // built-in toJson and fromJson functions available

Just like you have static Entry functions to convert JSON and XML to/from hierarchical Entry objects. I've written a series of tutorials about hierarchical data handling on tdiingoutloud.com
s***@fusionfuzion.com
2017-04-29 02:46:07 UTC
Permalink
Raw Message
Eddie Hartman於 2017年4月28日星期五 UTC+8下午2時46分55秒寫道:
Post by Eddie Hartman
Post by Steven
Hi all,
I would like to ask if there is any sample to describe how to post a JSON data as a http body through HTTPClientConnector in a script. My case needs to update asset properties in QRadar through SSL and SEC token.
/* script */
var assetId = 1442;
var updateProperties = [];
updateProperties.push({
"type_id" : 1002,
"value" : "testing"
})
var json = { "properties" : updateProperties };
var QRadarHost = system.getTDIProperty("host");
var token = system.getTDIProperty("token");
request.setAttribute("http.url","https://"+QRadarHost+"/api/asset_model/assets/"+assetId);
request.setAttribute("http.method","POST");
request.setAttribute("http.body", json);
request.setAttribute("http.Accept","text/plain");
request.setAttribute("http.Content-Type","application/json");
request.setAttribute("http.Version","8.0");
request.setAttribute("http.cookie","null");
request.setAttribute("http.Allow-Experimental","true");
request.setAttribute("http.Allow-Provisional","true");
request.setAttribute("http.SEC", token);
response = HConn.queryReply(request);
task.dump(response);
Many thanks
request["http.body"] = toJson(json); // built-in toJson and fromJson functions available
Just like you have static Entry functions to convert JSON and XML to/from hierarchical Entry objects. I've written a series of tutorials about hierarchical data handling on tdiingoutloud.com
Appreciate your help, I have tried your suggestion with work.fromJSON and work.toJSON but none of them works properly. I also used the function com.ibm.json.java.JSONObject.parse(jsonString) to convert a string into JSON object but it fails again. Hence, I wrote a web server to receive the post request and found out the body is empty.

However, my testing server does receive the json if I use "File to HTTP body" in HTTPClientConnector. I put the json in a file and use connector.setParam("inbody", fileName), so it can post a JSON to server. Nevertheless, it doesn't seem a correct way to do it.

BTW, I did read your tutorials before and they are helpful, thank you so much and have a nice day!
Eddie Hartman
2017-04-30 18:39:17 UTC
Permalink
Raw Message
Post by s***@fusionfuzion.com
Eddie Hartman於 2017年4月28日星期五 UTC+8下午2時46分55秒寫道:
Post by Eddie Hartman
Post by Steven
Hi all,
I would like to ask if there is any sample to describe how to post a JSON data as a http body through HTTPClientConnector in a script. My case needs to update asset properties in QRadar through SSL and SEC token.
/* script */
var assetId = 1442;
var updateProperties = [];
updateProperties.push({
"type_id" : 1002,
"value" : "testing"
})
var json = { "properties" : updateProperties };
var QRadarHost = system.getTDIProperty("host");
var token = system.getTDIProperty("token");
request.setAttribute("http.url","https://"+QRadarHost+"/api/asset_model/assets/"+assetId);
request.setAttribute("http.method","POST");
request.setAttribute("http.body", json);
request.setAttribute("http.Accept","text/plain");
request.setAttribute("http.Content-Type","application/json");
request.setAttribute("http.Version","8.0");
request.setAttribute("http.cookie","null");
request.setAttribute("http.Allow-Experimental","true");
request.setAttribute("http.Allow-Provisional","true");
request.setAttribute("http.SEC", token);
response = HConn.queryReply(request);
task.dump(response);
Many thanks
request["http.body"] = toJson(json); // built-in toJson and fromJson functions available
Just like you have static Entry functions to convert JSON and XML to/from hierarchical Entry objects. I've written a series of tutorials about hierarchical data handling on tdiingoutloud.com
Appreciate your help, I have tried your suggestion with work.fromJSON and work.toJSON but none of them works properly. I also used the function com.ibm.json.java.JSONObject.parse(jsonString) to convert a string into JSON object but it fails again. Hence, I wrote a web server to receive the post request and found out the body is empty.
However, my testing server does receive the json if I use "File to HTTP body" in HTTPClientConnector. I put the json in a file and use connector.setParam("inbody", fileName), so it can post a JSON to server. Nevertheless, it doesn't seem a correct way to do it.
BTW, I did read your tutorials before and they are helpful, thank you so much and have a nice day!
Thanks, Steven. Are you scripting this stuff, are you using the Parser connected to the HTTP Connector? This is seldom a good idea, in particular for input. Like you said, sometimes you don't get valid xml (like lazy html) or json or even a response. That's why I would use the Parser FC with the JSON Parser configured to parse out the hierarchical data. Usually I script it directly:

hEntry = work.fromXML(work.getString("http.bodyAsString"), "*", "");

The fromXML() call returns an hEntry (hierarchy is fun to spell :) which means that like work.toString() gets you a readable representation (JSON-like) view of the data tree. The same is true for work.fromJSON().

I use script more and more to do any business logic, leaving components to handle the dirty work of session management and data marshalling to/from the connected system. Sure, sometimes I articulate branching and loops using Branch and Loop components so they show up in the AL tree view of the Configuration Editor. But as I said, scripting is often faster, easier to maintain over time, as well as to debug.
Loading...