How to decode REST API requestbody- ServiceNow- InternetVista integration

Blago
Kilo Contributor

Hi guys! I am trying to decode a message coming from InternetVista, because at the moment is coming at the following format which is horrible to be used as useful information:

u_alias=my+alias&u_is_success=true&u_url=https%3A%2F%2Fwww.yourwebsite.com&u_status=ok&u_is_error=false&u_response_code=200&u_response_message=OK+%28200%29&u_check_duration=10&u_checked_by=Roubaix%2FFrance&u_check_date=03%2F27%2F2019+15%3A52%3A59&u_source=InternetVista

My goal is to be decoded using URL decode function in service now, but unofrtunately without great success.

Part of my script below:

var requestBody = request.body;


var result = requestBody.dataString;

//gs.info("DIW InternetVista: " + result);

gs.info("InternetVista: "+ gs.urlDecodede(result));

 Afterward my point is to capture u_alias, is_success, u_url, u_status, u_is_error, response code and so on using regex, but this is not good idea, because i have monitoring more than 100 applications and definitely i have more than 100 cases especially for url

2 REPLIES 2

Calvaresi E_
Giga Expert
var json = result.split('&').map(function(i) { return i.split('=');}).reduce(function(m,o){ m[o[0]] = o[1]; return m;},{});

This is a single line to convert URI to JSON. JSON are much easier to parse. You will have something like {'uriParameter':'uriValue', [...]}

Cheers

Blago
Kilo Contributor

Thanks Edoardo, but this is also don't work what i have on InternetVista i set Parameters:

u_alias=@ALIAS@
u_is_success=@IS_SUCCESS@
u_url=@DISPLAY_NAME@
u_status=@STATUS@
u_is_error=@IS_ERROR@
u_response_code=@RESPONSE_CODE@
u_response_message=@RESPONSE_MESSAGE@
u_check_duration=@CHECK_DURATION@
u_checked_by=@CHECKED_BY@
u_check_date=@CHECK_DATE@
u_source=InternetVista

and Request Headers:

Content-Type=application/json ( Working only with json and xml)

For some reason is coming in ServiceNow as format which i posted in previous post. My point is to Decode all information with URL encode, because only with this type i am able to use clean format of message. I want to avoid using RegEx step because we have a hundred various URL's 

var requestBody = request.body;
var result = requestBody.dataString;
var json = result.split('&').map(function(i) { return i.split('=');}).reduce(function(m,o){ m[o[0]] = o[1]; return m;},{});

// gs.info("InternetVista: "+ gs.urlDecode(result));

var u_alias = (/u_alias=([^&]+)/);
var u_url = (/(www.\w*.com)/);
var u_status = (/u_status=([^&]+)/);
var u_response_code = (/u_response_code=([^&]+)/);
var u_response_message = (/u_response_message=(\w*)/);
var u_is_error = (/u_is_error=([^&]+)/);

 

Using URL Decode: ( That is the main goal to transform information to that format)

u_alias=my alias&u_is_success=true&u_url=https://www.yourwebsite.com&u_status=ok&u_is_error=false&u_response_code=200&u_response_message=OK (200)&u_check_duration=10&u_checked_by=Roubaix/France&u_check_date=03/27/2019 15:52:59&u_source=InternetVista