HTTP POST to external url - ServiceNow Community

HTTP POST to external url

xMTinkerer
Giga Expert

Hello!
I am looking to send an HTTP POST to an external web site from SNC. I am slightly new to SNC so I have 2 questions:

1) What is the syntax for making an HTTP POST request?
2) How can I test it? Is there any other method to run arbitrary script than the Script - Background?

Thanks!
--- nomad

6 REPLIES 6

jfarrer
Mega Guru

This should get you started. It is basically just the Java HttpClient library so the javadocs have been helpful to us in figuring out obscure details.



var client = new Packages.org.apache.commons.httpclient.HttpClient();
var post = new Packages.org.apache.commons.httpclient.methods.PostMethod("http://www.exampledestination.com/page_to_post_to.html");
post.addParameter("parameter_name", "parameter value");
var returnCode = client.executeMethod(post);
var output = "" + post.getResponseBodyAsString();


We've used this code in business rules, workflow run script activities, as well as in building our own custom activity.

Are you looking for how to test that you're sending what you're expecting to send or to be able to just more easily test a call to a web page or web service?


Awesome! Thanks! I didn't know I could import Java libraries- I am rather new to SNC but can definitely code my way around a lot of things.

As for testing, I am just looking to test functionality (modular like) before I put it someplace it could break something. I like to test each piece before I put it into the bigger part. So testing functions before I put them into methods of the objects in the Script Include. Is the Script - Background the best place to do that?


That's what I do. I use the "Script - Background" module all of the time to test my reusable functions, code snippets, etc.

-John


I'm actually working on another order flow today that uses this code and am using the Script - Background to test it. I've put the script into a class in a Script Include and then I'm calling the parts that I need to test from the Script - Background page.