I heard that Groovy has a built-in REST/HTTP client. The only library I can find is HttpBuilder, is this it?
Basically I'm looking for a way to do HTTP GETs from inside Groovy code without having to import any libraries (if at all possible). But since this module doesn't appear to be a part of core Groovy I'm not sure if I have the right lib here.
You can take advantage of Groovy features like with(), improvements to URLConnection, and simplified getters/setters:
GET:
POST:
Note, the POST will start when you try to read a value from the HttpURLConnection, such as
responseCode
,inputStream.text
, orgetHeaderField('...')
.Native Groovy GET and POST
The simplest one got to be:
If your needs are simple and you want to avoid adding additional dependencies you may be able to use the
getText()
methods that Groovy adds to thejava.net.URL
class:If you are expecting binary data back there is also similar functionality provided by the
newInputStream()
methods.HTTPBuilder is it. Very easy to use.
It is especially useful if you need error handling and generally more functionality than just fetching content with GET.
I don't think http-builder is a Groovy module, but rather an external API on top of apache http-client so you do need to import classes and download a bunch of APIs. You are better using Gradle or
@Grab
to download the jar and dependencies:Note: since the CodeHaus site went down, you can find the JAR at (https://mvnrepository.com/artifact/org.codehaus.groovy.modules.http-builder/http-builder)