Grails RestBuilder simple POST example

后端 未结 3 1710
谎友^
谎友^ 2020-12-29 07:17

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 erro

相关标签:
3条回答
  • 2020-12-29 07:45

    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
    
    0 讨论(0)
  • 2020-12-29 08:02

    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
    
    0 讨论(0)
  • 2020-12-29 08:03

    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
    
    0 讨论(0)
提交回复
热议问题