Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

XMLHttpRequest();

ravi1021
Kilo Contributor

Hi Team ,

Can you please guide me how to call

var client=new XMLHttpRequest();

from Script Include, i have few parameters to be added to api call, so i cant use Rest Outbound here,

please suggest me

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ravi,



If this is a URL api then you can try below code in background script



var httpclient = Packages.org.apache.commons.httpclient;


var HttpClient = httpclient.HttpClient;


var UsernamePasswordCredentials = httpclient.UsernamePasswordCredentials;


var AuthScope = httpclient.auth.AuthScope;


var PostMethod = httpclient.methods.PostMethod;



// consider end point url as http://example.com/p.php



var client = new HttpClient();


var postMethod = postMethod = new PostMethod('endpoint url');


postMethod.addParameter("A", "Test");


postMethod.addParameter("B", "Test1");


postMethod.addParameter("C", "Test User8");


postMethod.addParameter("D", "TestUser8@example.com");



// these parameters will be replaced in the URL as below



http://example.com/p.php?A=Test&B=Test1&C=Test User8&D=TestUser8@example.com



var status = client.executeMethod( postMethod );


var result = (status + "\n" + postMethod.getResponseBodyAsString()).replace(/</g, "&"+"lt;");


gs.print(result);


postMethod.releaseConnection();



Mark my reply as Correct and also hit Like and Helpful if you find my response worthy.


Thanks


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader