Grails RestBuilder simple POST example

后端 未结 3 1725
谎友^
谎友^ 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
    

提交回复
热议问题