How can i do a POST request to External API from a Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2016 06:58 AM
Hi
i m trying to access an external api from client script ajax but getting into CORS issues. Is it possiblle that i can call the external API from Business Rule ?
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2016 07:02 AM
It is! You can do that by creating a REST Message record and setting up the authentication. Once you set everything up ServiceNow will generate the code you need to put in the business rule automatically.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2016 12:50 AM
Thanks Brad for your time, below is my Client script which is working
var settings = {
"crossDomain": true,
"url": "https://xyz.com/helloworld",
"type": "POST",
"headers": {
"Content-Type": "application/json"
},
"data": "{\r\n \"operation\": \"echo\",\r\n \"payload\": {\r\n \"somekey1\": \"somevalue1\",\r\n \"somekey2\": \"somevalue2\"\r\n }\r\n}"
};
$j.ajax(settings).done(function (response) {
alert(response);
console.log(response);
});
but when i try to use it by creating a REST message it does not return any thing, here is the code i am using in the BR
try {
var r = new sn_ws.RESTMessageV2('TD - Test', 'post');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.addInfoMessage("responseBody: "+responseBody);
gs.addInfoMessage("httpStatus: "+httpStatus);
}
catch(ex) {
var message = ex.getMessage();
gs.addErrorMessage("error:"+message);
}
Regards
