Hi Guys.In this blog I am going to share some details of Rest Client Builder plugin in grails.
This is my previous blog in which I used java core api for httpclient. In this blog I will demonstrate a similar type request functionality using Rest Client Builder plugin.
Let say we have a request for a service in CURL as :
curl -H "Content-Type: application/x-www-form-urlencoded" -X POST -u myusername:mypassword -d '{"name":"davy jones" , "email":"[email protected]"}' https://myApp.com/api/v1/json
Code in action-controller for using the service
def restClientAction(){ String username="myusername" String password="mypassword" String restUrl="https://myApp.com/api/v1/json" RestBuilder rest = new RestBuilder() String auth=new StringBuffer(username).append(":").append(password).toString(); byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII"))); String authHeader = "Basic "+new String(encodedAuth) RestResponse resp = rest.put(restUrl) { headers.'X-Stream' = 'true' accept("application/json") contentType("application/x-www-form-urlencoded") header 'AUTHORIZATION', authHeader json{ name = "davy jones" email = "[email protected]" } } println "resp : "+resp.json println "status : "+resp.getStatus() }
For more information on this plugin see this
Thanks
Akash Sharma
More From Oodles
Ready to innovate? Let's get in touch
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akash Sharma
Akash is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Akash loves playing Cricket and Tennis