Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

RAHUL Khanna1
Mega Guru

gr.assignment_group.toString()

 

Try Once where ever you are usning gr.object, use gr.object.toString();

same result, 400 

Thanks
AS

simonbergstedt
Tera Guru

I assume that you are not getting this error when testing it directly from the Outbound Rest Message you created?

I tried testing outbound rest message with the above mentioned JSON string as content. But there is same result.

Thanks
AS