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

James,

Do you know how to use the post.addParameter method to include a JSON object. The JSON object is already formatted for consumption by the 3rd-party web service and includes several parameters.


Depending on what the 3rd-party web service is expecting you would do one of a couple things. I haven't actually done this myself, but based on my experience and the documentation I believe you should be able to do it.

If the 3rd-party web service is expecting it as a parameter then you can just use the addParameter method and name the parameter to whatever is expected.

If it is expecting the JSON string as the entire body of the request then you would need to use the following in place of the post.addParameter line:



var request_body = new Packages.org.apache.commons.httpclient.methods.StringRequestEntity("json_string");
post.setRequestEntity(request_body);


According to the documentation there is a deprecated method of post.setRequestBody("string") that could be used. I don't know the version of HTTPClient that SNC uses so you may need to use this if the first doesn't work.

Also if you have an actual JSON object as opposed to a JSON formatted string then you would need to spit that out to a string for it to work.