Outbound Web Service -- HTTP 400 Bad Request

fbs
Giga Expert

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");

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);
 
I've  also tried just setting the body to text, and using setStringParameter.  I always get an HTTP 400.  It says the body is  {"aID":"AINST0010019","eMail":"foo@bar.com"}  which looks like good JSON, and is the same as when I run postman, which works.  I tried running the debugger but there is no way to start it up.  F2 does nothing. 
 
I have only a developer instance.  Does anyone know why I get HTTP 400 when I run from ServiceNow?  Thanks
 
            
 
1 ACCEPTED SOLUTION

Karan Chhabra6
Mega Sage
Mega Sage

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:

KaranChhabra6_0-1688703603444.png

 

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

 

View solution in original post

2 REPLIES 2

Karan Chhabra6
Mega Sage
Mega Sage

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:

KaranChhabra6_0-1688703603444.png

 

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

 

fbs
Giga Expert

Yes -- that was it.  Thanks a lot for the help.