Code

Under this page, I am keeping some useful code organised by technology/topic. See the sub-pages in the menu to the left.

OBS. My Heroku instance: http://gentle-gorge-5954.herokuapp.com

https://imgur.com/a/VBA1X

My code 'article' follow the format shown in the little example below.

Sample: Groovy HTTP Request example:

Suppose you need to make a HTTP Request to get some data from an online API. You're interested in inspecting the response headers as well as the response text so you can understand the API better. If you're like me and most people, you want this to be as easy and painless as possible, so you decide to use Groovy, of course! All you need to do (actually, a little more than you need to) is write something very similar to this: 

def connection = new URL( 'http://vecka.nu' ).openConnection()

// not required usually, but this demonstrates how you can set request headers

connection.addRequestProperty 'User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22'

if (connection.responseCode == connection.HTTP_OK) {

   connection.inputStream.eachLine {

       println "R    ${it}"

   }

} else {

   println "ERROR ${connection.responseCode}"

}

connection.headerFields.each {

   println "H      ${it}"

}

I prepended the lines which are from the response text with a 'R', and response headers with a 'H'. Job done!

However, you may be interested only in the response text. In that case, and if you don't need to do error handling, try this:

println 'http://vecka.nu'.toURL().text

Cool Groovy one-liner, as we've become used to expect...