The CreatorCon Call for Content is officially open! Get started here.

Call the REST API inside a Script Include

Julien GOUPIL
Tera Contributor

Hello,

 

I saw that I could retreive an RITM informations using the REST API, and I have no problem retreiving them using the link in my browser:

 

(mySNow.com/api/now/table/sc_req_item?sysparm_query=number%RITMXXXX&sysparm_fields=priority)

 

but I am unable to get the same Json data when I'm inside a Script Include.

I think it's possible but I guess I'm not doing it in the proper way.

 

I already tried the js "fetch()" with the rights header but didn't work.

 

If you ever had the case, let me know how did you do !

Thanks,

Regards,

1 ACCEPTED SOLUTION

Harshal Aditya
Mega Sage

Hi @Julien GOUPIL ,

 

Hope you are doing well.

 

I tried the below code in background script and seems to be working. You can adjust this code to use in script include.

 

var sm = new sn_ws.RESTMessageV2();
sm.setEndpoint("your rest end point URL");
sm.setHttpMethod('GET');
var user = 'xxxx';     // The user ID should have access to the ritm table
var password = 'xxxx';
sm.setBasicAuth(user,password);
sm.setRequestHeader("Accept","application/json");
var response = sm.execute();
gs.log(response.getBody());       // Will get you JSON data

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Harshal

 

View solution in original post

1 REPLY 1

Harshal Aditya
Mega Sage

Hi @Julien GOUPIL ,

 

Hope you are doing well.

 

I tried the below code in background script and seems to be working. You can adjust this code to use in script include.

 

var sm = new sn_ws.RESTMessageV2();
sm.setEndpoint("your rest end point URL");
sm.setHttpMethod('GET');
var user = 'xxxx';     // The user ID should have access to the ritm table
var password = 'xxxx';
sm.setBasicAuth(user,password);
sm.setRequestHeader("Accept","application/json");
var response = sm.execute();
gs.log(response.getBody());       // Will get you JSON data

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Harshal