Issue with REST API

Sohithanjan G
Kilo Sage
Kilo Sage

Hello,

 

I'm currently working on integrating ServiceNow with an external system using REST APIs, and I've encountered an issue. I'm making a REST API call from ServiceNow to fetch data, but it's returning unexpected results.

 

Here's a snippet of my code:


var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.example.com/data', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Bearer [insert_your_token_here]');

xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
// Process the response data
}
};

xhr.send();


The issue might be related to authentication or the way I'm handling the response. Could someone please guide me on the best practices for making REST API calls in ServiceNow and how to handle the responses correctly?

Thank you for your assistance!

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)
1 ACCEPTED SOLUTION

Anand Kumar P
Giga Patron
Giga Patron

Hi @Sohithanjan G ,

Hello,

It looks like you're on the right track with your REST API call. However, when working with ServiceNow, it's often recommended to use ServiceNow's GlideAjax or REST API capabilities for a more standardized and integrated approach.

Here's an example of how you can use ServiceNow's REST API:

var endpoint = 'https://api.example.com/data';
var headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer [insert_your_token_here]'
};

var response = sn_ws.REST.get(endpoint, null, headers);
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

if (httpStatus === 200) {
var parsedResponse = JSON.parse(responseBody);
// Process the parsed response data
} else {
// Handle error cases
gs.error('Failed to fetch data. HTTP Status: ' + httpStatus + ', Response: ' + responseBody);
}
This code uses the sn_ws.REST API, which is a ServiceNow-provided utility for making REST API calls. Make sure to replace [insert_your_token_here] with your actual API token.

Remember to check the ServiceNow documentation for any specific requirements or considerations for the particular REST API you are working with.

 

I hope this helps! Let me know if you have further questions.
Thanks,

Anand

View solution in original post

2 REPLIES 2

Peter Bodelier
Giga Sage

Hi @Sohithanjan G,

 

I would recommend you to use REST Message records, or REST steps in actions in FLow Designer instead of doing this scripted. These methods will guide you themselves.

 

See this documentation for more information:

Create a REST message (servicenow.com)

REST step (servicenow.com)

 

If you really must use script, then please leverage the ServiceNow provided Methods for this.

Recordless RESTMessageV2 example (servicenow.com)

Scripting outbound REST (servicenow.com)

 

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Anand Kumar P
Giga Patron
Giga Patron

Hi @Sohithanjan G ,

Hello,

It looks like you're on the right track with your REST API call. However, when working with ServiceNow, it's often recommended to use ServiceNow's GlideAjax or REST API capabilities for a more standardized and integrated approach.

Here's an example of how you can use ServiceNow's REST API:

var endpoint = 'https://api.example.com/data';
var headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer [insert_your_token_here]'
};

var response = sn_ws.REST.get(endpoint, null, headers);
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

if (httpStatus === 200) {
var parsedResponse = JSON.parse(responseBody);
// Process the parsed response data
} else {
// Handle error cases
gs.error('Failed to fetch data. HTTP Status: ' + httpStatus + ', Response: ' + responseBody);
}
This code uses the sn_ws.REST API, which is a ServiceNow-provided utility for making REST API calls. Make sure to replace [insert_your_token_here] with your actual API token.

Remember to check the ServiceNow documentation for any specific requirements or considerations for the particular REST API you are working with.

 

I hope this helps! Let me know if you have further questions.
Thanks,

Anand