Groovy HTTP Access

Last updated on 23 April, 2024

The LogicMonitor Collector has a number of helper methods to assist with using HTTP within Groovy. You might need to use Groovy / HTTP methods to:

  • fetch data from a webpage and process the results
  • perform multi-step web transactions  e.g. retrieve an authorization cookie in one transaction, and use that cookie to authenticate subsequent transactions

Note that if you’re writing a DataSource to pull data from a single webpage, you can do so with the Webpage collection mechanism.

Groovy Method Reference

Object Instantiation

HTTP.open(host, port, [isSSL]) instantiate an HTTP session object for use across multiple HTTP transactions

  • string host – hostname to connect to
  • int port – port to connect to
  • boolean isSSL – whether to use SSL or not; defaults to false unless port is 443
  • returns object httpClient the httpClient object

Object Methods

setAuthentication(user,pass) Set the authentication information for basic authentication for get and post methods

  • string user – username
  • string pass – password
  • returns Null

setConnectTimeout(milliseconds) sets the connection timeout for the initial TCP connection. Defaults to settings in the collector.conf file.

  • int milliseconds – the connection time in milliseconds

setReadTimeout(milliseconds) sets the read timeout (time to complete the request)  Defaults to settings in the collector.conf file.

  • int milliseconds – the read time in milliseconds

setFollowRedirect(follow) Normally the httpclient will transparently follow redirects. You can set this to false to not follow, and return the redirect headers.

  • boolean follow – whether to follow redirects or not. Defaults to true.

setHTTPProxy(host, port [,user, pass]) Specify an HTTP proxy to use for object get and post requests. If user and password are passed with credentials, only support BASIC authentication method

  • string host – the http proxy host
  • integer port – the http proxy port
  • optional string user – the http proxy username
  • optional string pass – the http proxy password

get(url, [headers])  do an HTTP GET on the provided url and return the entire HTTP response

  • string url – the target url
  • optional java.util.Map headers map of headers (requires collector 25.100 or later to support passing headers with the get)
  • return string response the entire HTTP response

e.g. response=httpClient.get(url,[“Accept” : “text/plain, text/html”])

post(url, payload, headers) do an HTTP POST to the provided url and return the entire response

  • string url – the target url
  • string payload – the post data payload
  • map headers – a map containing one or more HTTP POST headers
  • return string response the entire HTTP response

getHeaders() return only the HTTP headers of the last call

getHeader(string) return a specific header named string from the last call. e.g. println httpClient.getHeader(“Set-Cookie”)

getResponseBody() return only the HTTP body of the last call

getStatusCode() return only the HTTP status code of the last call as an integer.

getTimeStatistics() returns an array of two longs, containing the connect time (the time to complete the TCP and HTTPS establishment – this is different than the time set in setConnectTimeout) and read time (total time to complete the request) of the last request in milliseconds.

close() close and release the HTTP object. If you fail to do so, the collector will clean up and release the resource for you – but it’s always good practice to release resources 

Example – POST data to an HTTP API and test the Response

In this example, we do an HTTP POST to a server to authenticate a session. The session cookie will be automatically used in subsequent GET calls that use the same HTTP object.

// instantiate an http client object for the target system
ip="mydevice.com"
httpClient = HTTP.open(ip, 443);

// use an authentication API call to initiate a session
// specify the url to which we want to post
url = "https://"+ip+"/api/v310/session/login";
def payload = '{"username":"myusername","typeId":"com.tintri.api.rest.vcommon.dto.rbac.
    RestApiCredentials","password":"mypassword"}';
 
// do the post
def postResponse = httpClient.post(url, payload,["Content-Type":"application/json"]);
// does the response indicate a successful authentication?
if ( !(httpClient.getStatusCode() =~ /200/) ) 
{
    // no -- report an error, and return a non-zero exit code
    println "authentication failure";
    return(1);
}
// we are now authenticated. Subsequent GETs with the httpClient will pass in the session cookie 
url="https://"+ip+"/api/info";
def getResponse=httpClient.get(url);
// print some data
println httpClient.getResponseBody();

Example: Use Basic authentication to access an image and time the get

import com.santaba.agent.groovyapi.http.*;

// instantiate an http client object for the target system
httpClient = HTTP.open("www.mysite.com",80);
// set basic authentication credentials 
httpClient.setAuthentication("myusername","mypassword");
url = "https://www.mysite.com/httpgallery/authentication/authenticatedimage/figure1.gif";
def getResponse=httpClient.get(url)
headers=httpClient.getHeaders();
println headers;
times=httpClient.getTimeStatistics();
println "Connect Time "+times[0];
println "Read Time "+times[1];
httpClient.close();
return 0;
In This Article