Facing Error executing REST request: Invalid uri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2023 11:24 PM
I am working on to authenticate using Bearer Token and send a GET request to a server. When i send the request using POSTMAN, it works perfect and there are no errors to receive the response. But, when i run the script in servicenow using Rest API explorer, it always returns the following error:
An error occurred while making the API request: com.glide.communications.ProcessingException: Error executing REST request: Invalid uri 'https://exmpale.com/ppm/rest/v1/projects?fields=name,code,description,scheduleFinish,scheduleStart,isActive&filter=(code = 'PR000058')': Invalid query
Here is my code:
(function process(request, response /*null when async*/ ) {
var data = [];
try {
var params = request.queryString.split("=");
var projectCode = params[1];
if (params[0] === "code") {
var details = getProjectDetails(projectCode);
return details;
} else if (params[0] !== "code") {
gs.info("Input parameter must be 'code'! Exiting");
response.setError(new sn_ws_err.NotFoundError("No result found against the passed parameter. Parameter must be 'code'. Exiting"));
return;
}
} catch (ex) {
response.setError(new sn_ws_err.BadRequestError("Bad Request, Missing parameter 'code' or invalid code passed. (Hint: code=Project Code) "));
}
})(request, response);
function getProjectDetails(projectCode) {
var url = "https://example.com/ppm/rest/v1/projects?fields=name,code,description,scheduleFinish,scheduleStart,isActive&filter=(code = '" + projectCode + "')";
gs.info("URL: " + url);
try {
var results_array = makeRequest(url);
if (!results_array || results_array.length === 0) {
gs.error("No results found for project code: " + projectCode);
return null;
}
var data = [];
var name = results_array[0].name;
var code = results_array[0].code;
var description = results_array[0].description;
var scheduleFinish = results_array[0].scheduleFinish;
var scheduleStart = results_array[0].scheduleStart;
var isActive = results_array[0].isActive;
gs.info("code : " + code);
gs.info("name : " + name);
gs.info("description : " + description);
gs.info("isActive : " + isActive);
gs.info("scheduleFinish : " + scheduleFinish);
gs.info("scheduleStart : " + scheduleStart);
data[0] = code;
data[1] = name;
data[2] = description;
data[3] = scheduleFinish;
data[4] = scheduleStart;
data[5] = isActive;
return data;
} catch (error) {
// Log the error message
gs.error("An error occurred while making the API request: " + error);
return null;
}
}
function makeRequest(url) {
var authToken = "AUTH_TOKEN";
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(url);
request.setHttpMethod('GET');
request.setRequestHeader("x-api-ppm-client", "servicenow");
request.setRequestHeader("Content-Type", "application/json");
request.setRequestHeader("Authorization", "Bearer "+authToken);
var response = request.execute();
// response.waitForResponse(5);
var httpStatus = response.getStatusCode();
var responseBody = response.getBody();
gs.info("Response Body = " + responseBody);
gs.info("Response Headers = " + response.getAllHeaders());
if (httpStatus === 401) {
// Authentication failed
gs.error("Authentication failed. Please check your token.");
throw new Error("Authentication failed");
} else if (httpStatus !== 200) {
// API request failed
gs.error("API request failed with status code: " + httpStatus);
throw new Error("API request failed");
}
var results = JSON.parse(responseBody);
gs.info("results._results = " + results._results);
return results._results;
}
When i send the request using POSTMAN on the same URl using the same auth token, it proceeds without any issues but generates error when running script on servicenow. What could go wrong with my code that does not let the request complete?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2023 11:32 PM
Hello @Jamshaid ,
Please use rest message for that and call that rest message using script.
Please mark as helpful and accept solution if it is helpful for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2023 11:42 PM
Hi Harsh
Thanks for the kind suggestion. Could you please point me to some resources or an example as I'm new to servicenow scripts and doesn't know much about it.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2023 11:34 PM
Definitely something wrong in the configuration
where is the code to get Auth token? you are directly using the token without invoking the 1st API endpoint to get the token
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2023 11:45 PM
Hi ankue
Thanks for pointing the token generator. I'm not currently using it. I will add that part later. I'm currently concerned about that strange error. I've tried every possibility to debug that I could but unable to get the issue. Same token and url work fine in postman with same headers