Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy built-in REST/HTTP client?

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.

like image 789
smeeb Avatar asked Sep 05 '14 19:09

smeeb


People also ask

How do you get the response body in groovy?

Try response. data. keySet() to get a list of top-level keys. Then response.

What is http builder?

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.


1 Answers

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()); } 
like image 174
Jim Perris Avatar answered Sep 21 '22 14:09

Jim Perris