Getting 400-bad request error, when post data via Outbound REST message

AirSquire
Tera Guru

I am trying to post data to another SNOW instance using table API(for Incident in my case).

1. I have created an Outbound REST message and a post method.

2. I wrote a Scheduled Job to call the rest message. In the script using .setRequestBody(), i set the body to a JSON, like, '{"caller_id":"Abel Tuter","short_description":"from REST POST123"}'. Here I am able to create a new incident with caller_id as Abel Tuter and Short Description as from REST POST123 and Status as 201 created.

3. Now I am using the below script to create a new JSON string

var jsonobj={};
var count;
var query = 'active=true^assignment_group!=ITIL^ORassignment_group=NULL^assignment_group!=NULL^assigned_to!=NULL^category!=NULL';
var gr = new GlideRecord('incident');
gr.addEncodedQuery(query);
gr.query();
while(gr.next()){
jsonobj += '{"assignment_group":"'+gr.assignment_group+'","Short Description":"'+gr.short_description+'"},';
}
count = '{"records":['+jsonobj.toString().substring(15,jsonobj.toString().length)+']}';
var avc=count.substring(0,count.length-3);

4. Now I am using this JSON string to set the request body, it is like: 
'{"description":"JSON String"}'

var obj = '{"description":"'+avc+']}"}';
try { 
 var r = new sn_ws.RESTMessageV2('outbound rest to send json', 'post');
	r.setRequestBody(obj);

 var response = r.execute();

This is giving me 400 bad request error. In the outbound HTTP requests log, the request body is like this:
{"description":"{"records":[{"assignment_group":"287ebd7da9fe198100f92cc8d1d2154e","Short Descriptio**body truncated**

And the response body is:
{"error":{"detail":"Cannot decode: java.io.StringReader@121c0da","message":"Exception while reading **body truncated**

If i run the below code in background script I get the response pasted below the code:

var jsonobj={};
var count;
var query = 'active=true^assignment_group!=ITIL^ORassignment_group=NULL^assignment_group!=NULL^assigned_to!=NULL^category!=NULL';
var gr = new GlideRecord('incident');
gr.addEncodedQuery(query);
gr.query();
while(gr.next()){
jsonobj += '{"assignment_group":"'+gr.assignment_group+'","Short Description":"'+gr.short_description+'"},';
}
count = '{"records":['+jsonobj.toString().substring(15,jsonobj.toString().length)+']}';
var avc=count.substring(0,count.length-3);
var obj = '{"description":"'+avc+']}"}';
gs.info(obj)


*** Script: {"description":"{"records":[{"assignment_group":"287ebd7da9fe198100f92cc8d1d2154e","Short Description":"Network file shares access issue"},{"assignment_group":"287ebd7da9fe198100f92cc8d1d2154e","Short Description":"Wireless access is down in my area"},{"assignment_group":"8a4dde73c6112278017a6a4baf547aa7","Short Description":"I can't launch my VPN client since the last software update"},{"assignment_group":"8a5055c9c61122780043563ef53438e3","Short Description":"Rain is leaking on main DNS Server"},{"assignment_group":"d625dccec0a8016700a222a0f7900d06","Short Description":"How do I create a sub-folder"},{"assignment_group":"d625dccec0a8016700a222a0f7900d06","Short Description":"I can't get my weather report"},{"assignment_group":"d625dccec0a8016700a222a0f7900d06","Short Description":"Request for a new service"},{"assignment_group":"8a4dde73c6112278017a6a4baf547aa7","Short Description":"Issue with email"},{"assignment_group":"8a5055c9c61122780043563ef53438e3","Short Description":"Network storage unavailable"},{"assignment_group":"8a5055c9c61122780043563ef53438e3","Short Description":"Can't access Exchange server - is it down?"},{"assignment_group":"8a4dde73c6112278017a6a4baf547aa7","Short Description":"Manager can't access SAP Controlling application"},{"assignment_group":"8a4dde73c6112278017a6a4baf547aa7","Short Description":"SAP Financial Accounting application appears to be down"},{"assignment_group":"8a4dde73c6112278017a6a4baf547aa7","Short Description":"The SAP HR application is not accessible"},{"assignment_group":"73046b15db801300dea6dc50cf9619a0","Short Description":"form 16"}]}"}

According to my understanding this JSON string should get posted to description field of my provider instance. But it is not. Any help will be appreciated.

Regards
AS

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Here you go. 

var jsonobj={};
var jsonArr=[];
var query = 'active=true^assignment_group!=ITIL^ORassignment_group=NULL^assignment_group!=NULL^assigned_to!=NULL^category!=NULL';
var gr = new GlideRecord('incident');
gr.addEncodedQuery(query);
gr.query();
while(gr.next()){
jsonobj={};
jsonobj["Assignment group"]= gr.getValue('assignment_group');
jsonobj["Short Description"]=gr.getValue('short_description');
jsonArr.push(jsonobj);
}
var obj={};
obj.description=JSON.stringify(jsonArr);
try { 
var r = new sn_ws.RESTMessageV2('outbound rest to send json', 'post');
r.setRequestBody(JSON.stringify(obj));
//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.message;
}

 

View solution in original post

19 REPLIES 19

Still the same result. It is copying the json as an Array:

[{Assignment group=287ebd7da9fe198100f92cc8d1d2154e, Short Description=Network file shares access issue}, {Assignment group=287ebd7da9fe198100f92cc8d1d2154e, Short Description=Wireless access is down in my area}, {Assignment group=8a4dde73c6112278017a6a4baf547aa7, Short Description=I can't launch my VPN client since the last software update}, {Assignment group=8a5055c9c61122780043563ef53438e3, Short Description=Rain is leaking on main DNS Server}, {Assignment group=d625dccec0a8016700a222a0f7900d06, Short Description=How do I create a sub-folder}, {Assignment group=d625dccec0a8016700a222a0f7900d06, Short Description=I can't get my weather report}, {Assignment group=d625dccec0a8016700a222a0f7900d06, Short Description=Request for a new service}, {Assignment group=8a4dde73c6112278017a6a4baf547aa7, Short Description=Issue with email}, {Assignment group=8a5055c9c61122780043563ef53438e3, Short Description=Network storage unavailable}, {Assignment group=8a5055c9c61122780043563ef53438e3, Short Description=Can't access Exchange server - is it down?}, {Assignment group=8a4dde73c6112278017a6a4baf547aa7, Short Description=Manager can't access SAP Controlling application}, {Assignment group=8a4dde73c6112278017a6a4baf547aa7, Short Description=SAP Financial Accounting application appears to be down}, {Assignment group=8a4dde73c6112278017a6a4baf547aa7, Short Description=The SAP HR application is not accessible}, {Assignment group=73046b15db801300dea6dc50cf9619a0, Short Description=form 16}]

Regards
AS

That is correct. It will be a json array, now all the backslashes are removed correct?

Hi Abhinay,

Yesterday, I didnot had access to other instance so I used GET method to find the created array and the value in description field(on 2nd SNOW instance) using your 1st script. It responded with '/' in the value of description field. But today, when I checked the incident in the 2nd instance itself, I can see both the scripts provided by you are sending the same data(which is Json array) and the description field has same value for both incidents.

Thanks
AS

You cannot just put multiple json objects together without pushing them in to an array. Do not forget to mark my response as correct so that it closes the thread.

RAHUL Khanna1
Mega Guru

please close this thread , if it is resolved