- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2022 07:40 PM
I made JSON on OpenWeatherMap like below and I tried to call REST Massage in Script-Background of ServiceNow by entering JSON's script below and clicking "Run Script".However I couldn't call REST Massage.
If someone knows solution, please let me know.Thanks.
[JSON]
try {
var r = new sn_ws.RESTMessageV2('application name, 'title');
r.setStringParameterNoEscape('timezoon', '322440');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.message;
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2022 07:55 PM
you need to set the end point.
(function execute() {
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setEndpoint('https://api.openweathermap.org/data/2.5/weather?zip='+ YOUR_ZIP_CODE +'&appid='+ YOUR_KEY);
restMessage.setHttpMethod('get');
var response = restMessage.execute();
var httpResponseStatus = response.getStatusCode();
if (httpResponseStatus == 200) {
var responseBody = response.getBody();
var responseObject = JSON.parse(responseBody);
// do what you need to do with the response
return body;
}
})()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2022 05:07 PM
Hi Haruna,
I've created a REST Message and ran the script.
1. REST Message. Created a REST Message named "OpenWeatherMap" with HTTP Method name "GET" (changed the default method name).
Executed the following script. Note that parameters to RESTMessageV2 are REST Message name and HTTP Method name that I've created above.
try {
var r = new sn_ws.RESTMessageV2('OpenWeatherMap', 'GET');
r.setStringParameterNoEscape('timezone', '322440');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.info(responseBody);
} catch (ex) {
var message = ex.message;
}
Execution result:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2022 01:48 AM
Thanks a lot!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2022 09:47 PM
The complete step is as follows.
- In the "Endpoint", use "${appid}" instead of hardcoding the application id in the url. Hardcoding app id or url in script is a bad practice.
- Click on "Auto-generate variables" under "Related Links". This will generate a new record under "Variable Substitution".
- Click "appid" and enter application id in the "Test value" field.
- Save the setting and click on "Test" link under Related Links to test endpoint.