Blog

  • Rest Client Builder plugin Grails

    Posted by Akash Sharma | Last Updated: 20-May-14

    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":"davy@gmail.com"}' 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 = "davy@gmail.com"
    		} 
    	}
    	println "resp : "+resp.json
    	println "status : "+resp.getStatus()
    }
    

     

    For more information on this plugin see this

     

    Thanks

    Akash Sharma

     

Tags: grails

View All PostsLeave a Comment
comments powered by Disqus

Mobile Applications

Video Content

Bigdata & NoSQL

SaaS Applications

Miscellaneous

Archives


Alexa Certified Site Stats for www.oodlestechnologies.com