Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails RestBuilder simple POST example

Tags:

rest

post

grails

I'm trying to do an OAuth2 user-credentials post to an OAuth2 service using the Grails RestBuilder plugin.

If I try to specify the post body as a map, I get an error about no message converters for LinkedHashMap.

If I try to specify the body as a String, the post goes through, but none of the variables are posted to the server action.

Here's the post:

RestBuilder rest = new RestBuilder()
def resp = rest.post("http://${hostname}/oauth/token") {
    auth(clientId, clientSecret)
    accept("application/json")
    contentType("application/x-www-form-urlencoded")

    // This results in a message converter error because it doesn't know how
    // to convert a LinkedHashmap
    // ["grant_type": "password", "username": username, "password": password]

    // This sends the request, but username and password are null on the host
    body = ("grant_type=password&username=${username}&password=${password}" as String)
}
def json = resp.json

I've also tried setting the urlVariables in the post() method call, but the username/password are still null.

This is a very simple post, but I can't seem to get it to work. Any advice would be greatly appreciated.

like image 962
Townsfolk Avatar asked Feb 13 '14 02:02

Townsfolk


3 Answers

I solved the problem by using a MultiValue map for the body.

RestBuilder rest = new RestBuilder()
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>()
form.add("grant_type", "password")
form.add("username", username)
form.add("password", password)
def resp = rest.post("http://${hostname}/oauth/token") {
    auth(clientId, clientSecret)
    accept("application/json")
    contentType("application/x-www-form-urlencoded")
    body(form)
}
def json = resp.json
like image 164
Townsfolk Avatar answered Nov 16 '22 04:11

Townsfolk


Following code works for Box connection. Spend few of hours figuring this out

    String pclient_id = grailsApplication.config.ellucian.box.CLIENT_ID.toString()
    String pclient_secret=grailsApplication.config.ellucian.box.CLIENT_SECRET.toString()
    String pcode = params.code

    log.debug("Retrieving the Box Token using following keys Client ID: ==>"+pclient_id+"<== Secret: ==>"+pclient_secret+"<== Code: ==>"+pcode)
    RestBuilder rest = new RestBuilder()

    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>()
    form.add("client_id", pclient_id)
    form.add("client_secret", pclient_secret)
    form.add("grant_type", "authorization_code")
    form.add("code", pcode)
    def resp = rest.post("https://app.box.com/api/oauth2/token") {
        accept("application/json")
        contentType("application/x-www-form-urlencoded")
        body(form)
    }

    def js = resp.json.toString()
    println("sss"+js)
    def slurper = new JsonSlurper()
    def result = slurper.parseText(js)
    println("Message:"+result.error)

    render js
like image 45
Mak Kul Avatar answered Nov 16 '22 06:11

Mak Kul


I found out some very easy to perform such type of action

//Get
 public static RestResponse getService(String url) {
  RestResponse rResponse = new RestBuilder(proxy:["localhost":8080]).get(Constants.URL+"methodName")
  return rResponse
 }

 //POST : Send complete request as a JSONObject
 public static RestResponse postService(String url,def jsonObj) {
   RestResponse rResponse = new RestBuilder(proxy:["localhost":8080]).post(url) {
   contentType "application/json"
   json {  jsonRequest = jsonObj  }
  }
  return rResponse
 }

Method 1 :

def resp = RestUtils.getService(Constants.URL+"methodName")?.json
render resp as JSON

Method 2 :

JSONObject jsonObject = new JSONObject()
jsonObject.put("params1", params.paramOne)
jsonObject.put("params2", params.paramTwo)
def resp = RestUtils.postService(Constants.URL+"methodName", jsonObject)?.json
render resp as JSON
like image 28
Jeet Singh Parmar Avatar answered Nov 16 '22 06:11

Jeet Singh Parmar