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.
Try response. data. keySet() to get a list of top-level keys. Then response.
HttpBuilder-NG is a modern Groovy DSL for making HTTP requests. It requires Java 8 and a modern version of Groovy. It is built against Groovy 2.4. x, but it doesn't make any assumptions about which version of Groovy you are using.
Native Groovy GET and POST
// GET def get = new URL("https://httpbin.org/get").openConnection(); def getRC = get.getResponseCode(); println(getRC); if (getRC.equals(200)) { println(get.getInputStream().getText()); } // POST def post = new URL("https://httpbin.org/post").openConnection(); def message = '{"message":"this is a message"}' post.setRequestMethod("POST") post.setDoOutput(true) post.setRequestProperty("Content-Type", "application/json") post.getOutputStream().write(message.getBytes("UTF-8")); def postRC = post.getResponseCode(); println(postRC); if (postRC.equals(200)) { println(post.getInputStream().getText()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With