如何使用Groovy HTTPBuilder RESTClient构建HTTP post

如何使用Groovy HTTPBuilder RESTClient构建HTTP post,groovy,httpbuilder,Groovy,Httpbuilder,来自的post示例不适用于http builder 1.7.1 def msg = "I'm using HTTPBuilder's RESTClient on ${new Date()}" def resp = twitter.post( path : 'update.json', body : [ status:msg, source:'httpbuilder' ], requestContentType : URLENC ) assert

来自的post示例不适用于http builder 1.7.1

def msg = "I'm using HTTPBuilder's RESTClient on ${new Date()}"

def resp = twitter.post(
        path : 'update.json',
        body : [ status:msg, source:'httpbuilder' ],
        requestContentType : URLENC )

assert resp.status == 200
assert resp.headers.Status
assert resp.data.text == msg

def postID = resp.data.id
例外是

wslite.rest.RESTClientException: No such property: body for class: wslite.http.HTTPRequest

通过使用API,不清楚应该如何正确构造post。有什么想法吗?

基于这个异常,看起来您使用的是groovy wslite库,而不是HTTPBuilder。如果是这种情况,则应采取以下措施:

def resp = twitter.post(path: 'update.json') {
    urlenc status: msg, source:'httpbuilder'
}

在使用需要登录的httpbuilder调用页面时遇到困难。因此共享我的工作代码

    def http = new HTTPBuilder("http://localhost:8080/")
        def query = [ username: "testUsername", password:"testPassword"]
            http.request(Method.POST,ContentType.URLENC) {
                uri.path = "user/signin"
                uri.query = query
                headers['Content-Type']= "application/x-www-form-urlencoded" 
                response.success = {resp-> println resp.statusLine }
            }

希望这有帮助。

groovy http builder库在封面下使用wslite,但仅用于
RESTClient
类。http builder库中的
HTTPBuilder
类使用apache http builder。困惑了吗?啊,好的。如果您
println twitter.getClass()
它显示什么?如果答案是wslite.rest.RESTClient,那么我的答案应该仍然有效。这让我陷入了一个令人尴尬的简单问题:一个错误的导入语句!我应该使用
groovyx.net.http.RESTClient