Rest Client Builder plugin Grails

Posted By : Akash Sharma | 15-May-2014

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

 

About Author

Author Image
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

Request for Proposal

Name is required

Comment is required

Sending message..