- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 07:06 PM
I'm testing an outbound web service running from a business rule, and I always get a bad request. I have tested the same service with postman, and it works fine. I'm not sure why running it inside ServiceNow is different. Relevant code:
var r = new sn_ws.RESTMessageV2();
r.setEndpoint('https://mycompany.com/ws/MyService.svc/LogSomething');
r.setHttpMethod("post");
gs.log("Webhook body: " + body);
r.setRequestBody(body);
gs.log('httpStatus is ' + httpStatus);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 09:20 PM
Hi @fbs ,
Please set the header as well and try again:
r.setRequestHeader("Content-Type", "application/json");
Updated Script:
var r = new sn_ws.RESTMessageV2();
r.setEndpoint('https://mycompany.com/ws/MyService.svc/LogSomething');
r.setHttpMethod("post");
r.setRequestHeader("Content-Type", "application/json");
var aID = "AINST0010019";
var eMail = "foo@bar.com";
var obj = {"aID": aID, "eMail": eMail};
var body = JSON.stringify(obj);
gs.log("Webhook body: " + body);
r.setRequestBody(body);
var response = r.execute();
var httpStatus = response.getStatusCode();
gs.log('httpStatus is ' + httpStatus);
If you are using basic auth, please add one more line to it, refer to the image below:
If my answer has helped with your question, please mark it as correct and helpful
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2023 09:20 PM
Hi @fbs ,
Please set the header as well and try again:
r.setRequestHeader("Content-Type", "application/json");
Updated Script:
var r = new sn_ws.RESTMessageV2();
r.setEndpoint('https://mycompany.com/ws/MyService.svc/LogSomething');
r.setHttpMethod("post");
r.setRequestHeader("Content-Type", "application/json");
var aID = "AINST0010019";
var eMail = "foo@bar.com";
var obj = {"aID": aID, "eMail": eMail};
var body = JSON.stringify(obj);
gs.log("Webhook body: " + body);
r.setRequestBody(body);
var response = r.execute();
var httpStatus = response.getStatusCode();
gs.log('httpStatus is ' + httpStatus);
If you are using basic auth, please add one more line to it, refer to the image below:
If my answer has helped with your question, please mark it as correct and helpful
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2023 07:36 AM
Yes -- that was it. Thanks a lot for the help.