Grails REST客户端插件 - PUT正文内容

时间:2011-03-20 18:53:37

标签: rest grails grails-plugin httpbuilder

在插件方面以及HTTPBuilder方面,似乎缺乏文档。我试图通过put方法提交一些json,但它一直告诉我put()不喜欢我正在喂它的地图。

有没有人使用Grails REST Client插件获得PUT的示例?这是我尝试过的:

withHttp(uri: "http://foo/doo/roo") {
        def bodyContent = [
            pano: jsonText
        ]

        def json = put(body: bodyContent)

        if (json.stat == 'ok') {
          wsr.success = true
        }
}

错误:

No signature of method: com.wbr.pano.PanService.put() is applicable for argument types: (java.util.LinkedHashMap) values: [[body:
    {
      "class":"com.wbr.platform.Pano",
      "errorMessage":"null",
      "imageSize":0,
      "id":26,
      "completed":"2011-03-20 3:50:27.257",
      "downloading":"2011-03-20 3:49:12.269",
      "processing":"2011-03-20 3:49:42.911",
      "uploading":"2011-03-20 3:50:12.107"
    }
  ]]

1 个答案:

答案 0 :(得分:6)

HTTPBuilder没有 put 方法。尝试将 withHttp 更改为 withRest ,以便使用RESTClient执行语句。此外,我认为默认情况下正文被编码为URL编码,因此您可能需要指定 requestContentType:groovyx.net.http.ContentType.JSON 作为 put 。

import static groovyx.net.http.ContentType.*

withRest(uri: "http://foo/doo/roo") {
        def bodyContent = [
            pano: jsonText
        ]

        def json = put(body: bodyContent, requestContentType: JSON)

        if (json.status == 200) {
          wsr.success = true
        }
}