The CreatorCon Call for Content is officially open! Get started here.

Rest outbound call using authCode and Content-Type application/x-www-form-urlencoded

dave_edgar
Mega Guru

I need to send and outbound message to another system using an authCode and Content-Type application/x-www-form-urlencoded

I can send from Postman fine so I know that works but how can i create a business rule in SNow to send this

I've created a REST outbound (sys_rest_message) to the endpoint with Authentication Type = None

and created a POST HTTP method record again with Authentication Type = None but created a HTP Header of

authCode (value is the code provided and sed in postman)

Content-Type = application/x-www-form-urlencoded

BUT

the preview scripts doesn't give me what I need for the business rule i need to write:

try {

var r = new sn_ws.RESTMessageV2('test', 'post');

//override authentication profile

//authentication type ='basic'/ 'oauth2'

//r.setAuthentication(authentication type, profile name);

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

}

catch(ex) {

var message = ex.getMessage();

}

Am I doing this write, I've been told I can an example outbound payload script so I can then put the password in sys_properties but i need the script for to grab the field details for the payload.   new to api calls especially going out of SNow.

Help!

1 ACCEPTED SOLUTION

venkatiyer1
Giga Guru

Hi Dave,



Create a rest api with an endpoint specified and select basic authentication if you have user name and password.


Or as you have already done just add the authorization header.





Instead of sending as application/x-www-form-urlencoded i would send the content as application/json.


So in your rest call under post create two more headers.


Accept with value application/json


Content-Type with value application/json




From your business rule, call the following script


Replace Name of your rest API with the actual name.


Also replace the key and value of the content object according to what you are sending.




  var request = new sn_ws.RESTMessageV2("Name of your rest API", "post");


  var content = {


  'key': current.column_name.toString(),


  'key1': current.column_name.toString()


  };


  var requestBody = JSON.stringify(content);


  request.setRequestBody(requestBody);




  var response = request.execute();


  var responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();


  var status = response.getStatusCode();


View solution in original post

3 REPLIES 3

venkatiyer1
Giga Guru

Hi Dave,



Create a rest api with an endpoint specified and select basic authentication if you have user name and password.


Or as you have already done just add the authorization header.





Instead of sending as application/x-www-form-urlencoded i would send the content as application/json.


So in your rest call under post create two more headers.


Accept with value application/json


Content-Type with value application/json




From your business rule, call the following script


Replace Name of your rest API with the actual name.


Also replace the key and value of the content object according to what you are sending.




  var request = new sn_ws.RESTMessageV2("Name of your rest API", "post");


  var content = {


  'key': current.column_name.toString(),


  'key1': current.column_name.toString()


  };


  var requestBody = JSON.stringify(content);


  request.setRequestBody(requestBody);




  var response = request.execute();


  var responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();


  var status = response.getStatusCode();


Thanks Venkat.



I'll try and persist with json but I fear the company we have to send data to can't handle json, no idea why but previous attempts have all failed while using postman.   Can't even get them to whitelist SNows ip address atm, sigh.


venkatiyer1
Giga Guru

Hi Dave,



If you want to persist using form encoded type then change it to the instructions below



set the Content-Type in the rest post message to application/x-www-form-urlencoded




set the accept according to what you are expecting as response. If it is json, it should be application/json




var request = new sn_ws.RESTMessageV2("Name of your rest API", "post");


var requestBody = "key=" + current.column_name.toString + "&key1=" +current.column_name.toString;


request.setRequestBody(requestBody);




  var response = request.execute();


  var responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();


  var status = response.getStatusCode();