How to log Outbound Rest API call request body?

Deepak K
Mega Guru

Hello All,

I want to check the request payload of the REST API that is sending out from ServiceNow instance. Is there any way to log the request body?

 

Please find the below REST API request:

 var r = new sn_ws.RESTMessageV2('xxxxx', 'Create');
 r.setStringParameter('u_caller', 'web_test');
 r.setStringParameter('u_sub_category', 'Security gap');
 r.setStringParameter('u_correlation_display', 'BRCMINFY');
 r.setStringParameter('u_configuration_item', 'Cybersecurity Operations');
 r.setStringParameter('u_category', 'Security');
 r.setStringParameter('u_module', 'incident');
 r.setStringParameter('u_purpose', purpose);
 r.setStringParameter('u_contact_type', 'Integration');
 r.setStringParameter('u_state', current.state);
 r.setStringParameter('u_description', jsonEncode(current.description.replace(/"/g, "'") + ''));
 r.setStringParameter('u_short_description', current.getValue('short_description'));
 r.setStringParameter('u_impact', current.impact);
 r.setStringParameter('u_urgency', current.urgency);
 r.setStringParameter('u_correlation_id', current.sys_id);
 r.setStringParameter('u_assignment_group', current.getDisplayValue('assignment_group'));
r.setStringParameter('u_additional_comments',jsonEncode(current.comments.getJournalEntry(1) + ''));
r.setStringParameter('u_work_notes', jsonEncode(current.work_notes.getJournalEntry(1) + ''));
		r.setStringParameter('u_external_reference_number', current.getDisplayValue('number'));
		if(current.state == 6){
		r.setStringParameter('u_resolution_notes', current.getValue('close_notes'));
		r.setStringParameter('u_resolution_code', current.getValue('close_code'));
		}
		
		
 var response = r.execute();

Thanks,

Deepak K

1 ACCEPTED SOLUTION

Hi,

You can try to print the request body once you add the line which executed the API i.e. after

var response = r.execute();

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Hi Ankur,

Thanks. I got your point.

 

For the time being, we are logging into logs table using getRequestBody() method from RESTMessageV2 API.

for example,

var sm = new sn_ws.RESTMessageV2("<REST_message_record>","get");

......

var body = sm.getRequestBody();

 

But I'm getting the logs as below: 

"u_state":"${u_state}",
"u_assignment_group":"${u_assignment_group}",
"u_impact":"${u_impact}",
"u_urgency":"${u_urgency}",
"u_configuration_item":"${u_configuration_item}",
"u_category":"${u_category}",
"u_sub_category":"${u_sub_category}",
"u_business_service":"${u_business_service}",
"u_correlation_id":"${u_correlation_id}",
"u_correlation_display":"${u_correlation_display}",
"u_short_description":"${u_short_description}",
"u_description":"${u_description}",
"u_company":"${u_company}",

 

It is displaying the variables, not actual values.

 

Could you please help me how can we store that logs with actual values.

 

Thanks,

Deepak K

Hi,

You can try to print the request body once you add the line which executed the API i.e. after

var response = r.execute();

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

 

Thanks, it's working.

 

Thanks,

Deepak K

Glad to know.

Please mark response helpful as well.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader