{"id":14255,"date":"2014-06-25T13:39:14","date_gmt":"2014-06-25T08:09:14","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14255"},"modified":"2016-12-19T15:04:05","modified_gmt":"2016-12-19T09:34:05","slug":"using-groovys-http-builder-library","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-groovys-http-builder-library\/","title":{"rendered":"Using Groovy&#8217;s HTTP Builder Library"},"content":{"rendered":"<p>In my <a title=\"grails development services\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">Grails app<\/a> project, I had a requirement to make an HTTP call from the server\u2019s end. After a lot of searching, I found some old fashioned Java code to achieve what I was looking for. But I was looking for something more clean, concise and more importantly a groovier way of doing it. On searching a little bit more, I found the awesome feature of HTTPBuilder in Groovy &#8211; a simpler and cleaner way to make HTTP requests.<\/p>\n<p>There are two different way to make an HTTP call using the HTTPBuilder class:<\/p>\n<p><b>1) Using the \u201cget\u201d and \u201cpost\u201d methods<\/b><\/p>\n<p>HTTPBuilder class provides you with wrapper methods to make GET and POST calls easily to the URL of your choice. An example usage is given below:<\/p>\n<p>[code]<br \/>\nString url = \u201chttp:\/\/www.example.com\u201d<br \/>\nString uriPath = \u201c\/sample-rest-call<\/p>\n<p>HTTPBuilder http = new HTTPBuilder(url)<\/p>\n<p>http.get(path:uriPath, contentType: ContentType.TEXT, query: content) { resp, reader -&gt;;<br \/>\n     println \u00a0reader.text<br \/>\n     println resp.statusLine<br \/>\n}<\/p>\n<p>http.post(path:uriPath, body:content, requestContentType:URLENC) { resp, reader -&gt;;<br \/>\n\/\/content will be url encoded<br \/>\n     println \u00a0reader.text<br \/>\n     println resp.statusLine<br \/>\n     assert resp.statusLine.statusCode == 200<br \/>\n}<br \/>\n[\/code]<\/p>\n<p><b>2) Using the \u201crequest\u201d method <\/b><\/p>\n<p>This method is internally called by the \u201cget\u201d and \u201cpost\u201d methods discussed earlier. An example usage is given below:<\/p>\n<p>[code]<br \/>\nString url = \u201chttp:\/\/www.example.com\u201d<br \/>\nString uriPath = \u201c\/sample-rest-call<\/p>\n<p>HTTPBuilder http = new HTTPBuilder(url)<br \/>\nhttp.request(POST, JSON) {<br \/>\n     uri.path = uriPath<br \/>\n     send URLENC, content<br \/>\n     response.success = { resp, reader -&gt;;<br \/>\n          String jsonResponse = (reader.readLines().join() as String)<br \/>\n          println new JsonSlurper().parseText(jsonResponse)<br \/>\n     }<\/p>\n<p>     response.failure = { resp, reader -&gt;;<br \/>\n     log.error &quot;Request failed : [URI : ${uriPath}, Status: ${resp.status}]&quot;<br \/>\n     }<\/p>\n<p>     response.\u2019401\u2019= {resp -&gt;;<br \/>\n     println \u201cStatus 401 received\u201d<br \/>\n     }<\/p>\n<p>}<br \/>\n[\/code]<\/p>\n<p>As you can see in the above example, the HTTPBuilder provides a very nice DSL to deal with the general case of dealing with errors and also allows you to provide special handling for specific status codes like the 401 status code in the example given above.<\/p>\n<p>You can find more in-depth documentation about the HTTPBuilder class at the following link:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my Grails app project, I had a requirement to make an HTTP call from the server\u2019s end. After a lot of searching, I found some old fashioned Java code to achieve what I was looking for. But I was looking for something more clean, concise and more importantly a groovier way of doing it. [&hellip;]<\/p>\n","protected":false},"author":75,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":95},"categories":[7],"tags":[4840,1403,4298],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14255"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/75"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14255"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14255\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}