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
07-04-2024 06:03 AM
hello,
I am having same issue , Could you please advise on how did this problem resolved ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2024 09:28 PM
I had same problem which got fixed using encoded values
If there are any symbol as "baseurl?filter[tags]=xyz,abc" then ensure you replace [] with encoded values as "baseurl?filter%5Btags%5D=xyz,abc".. like you have () , so you need to use encoded value for these symbols