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 - 

    http://myApp.com/getData?param1=something&param2=something

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

      import groovyx.net.http.HTTPBuilder

      try{  
        def http = new HTTPBuilder('http://www.myApp.com')
		http.get( path : '/getData', query : [param1:something,param2: something] )
		{ resp ->
			 jsonResp = resp.entity.content.text
             println jsonResp
           }
       }
       catch(groovyx.net.http.HttpResponseException 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 is an experienced Backend Developer with several years of industry experience in the IT field. He possesses a wide range of skills, including expertise in Backend languages like Core Java, J2EE, Hibernate, Spring/Spring Boot, and Python. Manish is also proficient in relational databases such as MySQL, PostgreSQL, and Oracle. He has hands-on experience in API implementations, web services development, testing, and deployments. Manish has contributed to various internal and client projects, including PMO, Catalyst, Communication-Scaffold, Oodles-Dashboard, and Devops Support, delivering significant business value. He is known for his innovative mindset and excellent problem-solving abilities. He keeps himself updated with new technologies by reading about them. He is skilled at collaborating closely with clients to define project scope and requirements, establish project timelines and milestones, and manage expectations. Manish conducts regular project status meetings, ensuring regular updates to clients and stakeholders regarding project progress, risks, and issues. Additionally, he serves as a mentor and coach to junior developers, offering guidance on project management best practices and fostering their skills development.

Request for Proposal

Name is required

Comment is required

Sending message..