HTTP GET Request In Groovy Grails

Posted By : Manish Kumar Narang | 31-Dec-2017

Recently while working on a project, I had to make an HTTP GET call with parameters to a different server from the backend side. There is not much data available online to guide on this issue. But after doing some search, I got to know about the HTTPBuilder library through which these actions can be performed. 


To begin with, we need to first install the HTTPBuilder library. This itself became a task because i had some trouble in trying to install it. There are various modules available online but the one which worked for me is - 'org.codehaus.groovy.modules.http-builder:http-builder:0.7'. It is important to mention here that I am working with grails 2.2.4, Maybe different modules work for different versions.


So, to install HTTPBuilder library, we need to update the BuildConfig.groovy file - 

    dependencies {
		compile "org.codehaus.groovy.modules.http-builder:http-builder:0.7"

Now suppose, i want to make this call -

We can accomplish this from ther server side by writing this simple piece of code - 


        def http = new HTTPBuilder('')
		http.get( path : '/getData', query : [param1:something,param2: something] )
		{ resp ->
			 jsonResp = resp.entity.content.text
             println jsonResp
       catch( e){
             println e.toString()

Please note how the query parameters are passed. The closure 'resp.entity.content.text' gives you the raw response. If the response is in the form of JSON then the closure directly gives you a map of it which can used directly.

About Author

Author Image
Manish Kumar Narang

Manish Narang has skills in software development and is enthusiastic about web applications and loves to work in a team.

Request for Proposal

Name is required

Comment is required

Sending message..