RESTMessageV2 - execute() returns 404 when Rest API returns 302 Found response code.

Madhavi Devadk2
Tera Contributor

Hi 

 

We are executing one token API from the service now using RESTMessageV2 - execute().APIs return status is 302 with token information in cookies. But when we execute it using RESTMessageV2 - execute() from service now it returns 404 status and no cookies found with it.

 

When API returns the 302 status code it means redirecting to another URL and that is why we are getting 404 as a new URL not found. Hence we are unable to get the required information in response headers.

We are looking for a way how we can stop auto-redirect in case of response 302.

 

Or is there any way to execute the rest API which returns 302 so that we will get the required response without redirecting?

3 REPLIES 3

Amit Gujarathi
Giga Sage
Giga Sage

HI @Madhavi Devadk2 ,
I trust you are doing great.

When the API returns a status code of 302, it indicates that the request is being redirected to another URL. However, when you execute the API using RESTMessageV2's execute() function in ServiceNow, it returns a 404 status code, indicating that the new URL is not found and no cookies are present.

To address this issue, we need to prevent the automatic redirection when encountering a 302 response. We can achieve this by modifying the RESTMessageV2 configuration.

You can add the following code snippet before calling the execute() function to disable automatic redirection:

 

var request = new sn_ws.RESTMessageV2();
// Set up the REST message properties
// ...

// Disable automatic redirection
request.setFollowRedirects(false);

// Execute the REST message
var response = request.execute();

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



@Amit Gujarathi Thanks for the solution to try out. Will try and update here. I have checked this method on Service Now document but have not seen this.

@Amit Gujarathi 

I tried above solution in both global script and application script.

In application script I am getting exception : inside catchTypeError: Cannot find function setFollowRedirects in object [object RESTMessageV2].

and In global scope I am getting 404 response when I execute API with setFollowRedirects.

 

Please guide us on above.

 

Thanks,

Madhavi Devadkar.