The URI may be extended in the request-specific configuration using the getUri() method, which returns an instance of groovyx.net.http.UriBuilderpopulated with the URI information from the client configuration. This is useful to change the path or query string information of the URI for aspecific request, while leaving the base URI in the global configuration:
The JavaMail API support is optional, and requires that the JavaMail API library be on the classpath. Take a look at the ParsersSpec.groovy testcase for the full implementation and an alternate implementation without using the JavaMail API.
During testing or debugging of HTTPS endpoints it is often useful to ignore SSL certificate errors. HttpBuilder-NG providestwo methods of ignoring these issues. The first is via the configuration DSL using thegroovyx.net.http.util.SslUtils::ignoreSslIssues(final HttpObjectConfig.Execution) method.
This section contains some stand-alone script examples of how you can use HttpBuilder. You can copy them and run them inthe Groovy Shell or there are unit test versions for most of these examples in the ExamplesSpec.groovy file.
In the example we make a GET request to the a secondary Maven repository to fetch the main entry page for the groovy-allartifact, which has the license information on it. The page is returned and parsed into a JSoup Document which we can thenrun a CSS selection query on to extract the license information and display it. You will end up with:
Which uses the groovyx.net.http.optional.Download class toFile(HttpConfig, File) method to perform the downloading by providing a means ofstreaming the remote contents into the file. See the docs for the Download class for more options and details - the unit test, DownloadSpec alsohas some good usage examples for this functionality.
This example will show how to make a get request to a URI in groovy with HttpBuilder. HTTPBuilder provides an abstraction on top of apache HttpComponents which provides a low level set of components for interaction with HTTP and associated protocols. The HTTPBuilder api coupled with groovy makes it easy to fetch and parse information from a URL which is similar to a JavaScript ajax call. A comparable example we demonstrate how to make a get request with apache components in java.
In this snippet, a closure is defined with request information. You can set various pieces of the request or response such as headers, content type, paths and how to handle response behavior. We will specify the request method as a GET, content type as text, two closures to handle the response and location of groovy examples. The first closure to handle the response is to handle if it is a success and the second if the response fails. When the status code is 200 we will output response, content type and response contents text to the console. If it fails a message will appear with not found.
This snippet will show how to use the convenience method to perform an HTTP GET. Similar to above we will make a request to leveluplunch groovy examples while specifying the path and content type. Only major difference is we will output each header name/value and remove the response closure as the http.get method will only handle a successful response.
The quickest way to determine if the @Grab imported library works in Groovy 4 is to try it on Groovy 4. If you have Groovy installed locally on your machine, you can use the groovyconsole as described in the Apache Groovy documentation.
Several users import libraries with @Grab that allow them to make REST calls. ScriptRunner includes the org.codehaus.groovy.modules.http-builder library, which is documented here. This library can most likely do whatever you need to do concerning making external REST calls. This library has classes like groovyx.net.http.HTTPBuilder and groovyx.net.http.RESTClient. Both classes can be accessed with a simple import, with no @Grab calls required.