MyThinkPond

On Java, Python, Groovy, Grails, Spring, Node.js, Linux, Arduino, ARM, Embedded Devices & Web

Grails – Groovy – Alternative to HttpBuilder – adding headers to your HTTP request

Posted by Venkatt Guhesan on October 24, 2011

Developing with Grails and Groovy can be a blessing and and pain all at the same time. The development moves at a rapid rate but when you decide to include libraries that depend on other libraries, your pain starts to build up. For example, when you include the module “HttpBuilder”in your project you may run into issues with Xerces and xml-apis, especially when you attempt to deploy the WAR file under Tomcat. These libraries are included as part of Tomcat and so an older version of those classes may give you a heartburn.

If your objective is to use some raw HTTP classes to create your requests and responses, then you can use the basic URL class to do most of the raw connection options. Although using HttpBuilder makes it a clean implementation, the URL class gives you very similar power without all the overhead of including the dependency classes.


def urlConnect = new URL(url)
def connection = urlConnect.openConnection()
//Set all of your needed headers
connection.setRequestProperty("X-Forwarded-For", "<your ip address>")

if(connection.responseCode == 200){
responseText = connection.content.text
}
else{
println "An error occurred:"
println connection.responseCode
println connection.responseMessage
}

So the trick to the Groovy URL class is to use the “openConnection()” method and then gain access to some of the raw functionality.

Cheers.

Advertisements

7 Responses to “Grails – Groovy – Alternative to HttpBuilder – adding headers to your HTTP request”

  1. Twitter said

    Everyone loves it when people come together and share views.
    Great website, keep it up!

  2. King said

    You could certainly see your skills within the work
    you write. The arena hopes for more passionate writers such as
    you who aren’t afraid to mention how they believe. All the time go after your heart.

  3. Eddie said

    I got this website from my pal who told me on the topic
    of this web site and now this time I am visiting this
    web page and reading very informative posts here.

  4. Excellent blog here! Also your web site loads up fast!
    What host are you using? Can I get your affiliate link
    to your host? I wish my web site loaded up as quickly as yours
    lol

  5. Jaunita said

    Have you ever considered writing an ebook or guest authoring on other websites?

    I have a blog based upon on the same subjects you discuss and would love to have you share
    some stories/information. I know my readers would value your work.

    If you are even remotely interested, feel free to
    shoot me an e-mail.

  6. Nice blog here! Additionally your site so much up fast!

    What host are you the usage of? Can I get your affiliate hyperlink to your host?
    I desire my website loaded up as fast as yours lol

  7. Stewart said

    Hey! Someone in my Myspace group shared this site with us so I came to look it over.
    I’m definitely loving the information. I’m book-marking
    and will be tweeting this to my followers! Superb blog and terrific design.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: